View Javadoc

1   package org.apache.onami.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.onami.test.OnamiRunner;
23  import org.apache.onami.test.annotation.GuiceModules;
24  import org.apache.onami.test.annotation.Mock;
25  import org.apache.onami.test.data.HelloWorld;
26  import org.apache.onami.test.data.SimpleModule;
27  import org.apache.onami.test.data.TelephonService;
28  import org.easymock.EasyMock;
29  import org.junit.Assert;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  
33  import com.google.inject.Inject;
34  import com.google.inject.Injector;
35  
36  @RunWith( OnamiRunner.class )
37  @GuiceModules( SimpleModule.class )
38  public class InjectMockObjectTestCase
39      extends AbstractMockTestCase
40  {
41  
42      // Create and inject a simple EasyMock Strict mock
43      @Mock
44      private TelephonService telephonServiceMock;
45  
46      @Inject
47      Injector injector;
48  
49      @Inject
50      private HelloWorld helloWorld;
51  
52      @Test
53      public void testMock()
54      {
55          EasyMock.expect( providedMock.go() ).andReturn( "Ciao" );
56          EasyMock.replay( providedMock );
57  
58          Assert.assertNotNull( this.providedMock );
59          Assert.assertEquals( "Ciao", helloWorld.sayHalloByService() );
60          EasyMock.verify( providedMock );
61      }
62  
63      @Test
64      public void testMock2()
65      {
66          EasyMock.expect( providedMock.go() ).andReturn( "Ciao" );
67          EasyMock.replay( providedMock );
68  
69          Assert.assertNotNull( this.providedMock );
70          Assert.assertEquals( "Ciao", helloWorld.sayHalloByService() );
71          EasyMock.verify( providedMock );
72      }
73  
74      @Test
75      public void testStrickMock()
76      {
77          EasyMock.expect( telephonServiceMock.getTelephonNumber() ).andReturn( "1234567890" );
78          providedMock.call( "1234567890" );
79          EasyMock.expectLastCall().once();
80          EasyMock.replay( telephonServiceMock );
81          EasyMock.replay( providedMock );
82  
83          helloWorld.callHelloWorldTelephon();
84  
85          EasyMock.verify( telephonServiceMock );
86          EasyMock.verify( providedMock );
87  
88          // reset manually the mock object. Flag resettable is false!!!
89          EasyMock.reset( telephonServiceMock );
90      }
91  
92      @Test
93      public void testStrickMock2()
94      {
95          Assert.assertNotNull( telephonServiceMock );
96      }
97  
98  }