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 java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.onami.test.OnamiRunner;
26  import org.apache.onami.test.annotation.Mock;
27  import org.apache.onami.test.data.HelloWorld;
28  import org.apache.onami.test.data.Service;
29  import org.easymock.EasyMock;
30  import org.junit.Assert;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  
35  import com.google.inject.AbstractModule;
36  import com.google.inject.Inject;
37  import com.google.inject.Injector;
38  import com.google.inject.TypeLiteral;
39  
40  @RunWith( OnamiRunner.class )
41  public class InjectDependingMockObjectTestCase
42  {
43  
44      @Mock
45      static private Service service;
46  
47      @Inject
48      Injector injector;
49  
50      private HelloWorld helloWorld;
51  
52      @Before
53      public void setUp()
54      {
55          final List<Service> list = new ArrayList<Service>();
56          list.add( service );
57  
58          AbstractModule listAbstractModule = new AbstractModule()
59          {
60              @Override
61              protected void configure()
62              {
63                  bind( new TypeLiteral<List<Service>>()
64                  {
65                  } ).toInstance( list );
66              }
67          };
68  
69          Injector cInjector = injector.createChildInjector( listAbstractModule );
70          helloWorld = cInjector.getInstance( HelloWorld.class );
71          // required for optional dependencies
72          cInjector.injectMembers( helloWorld );
73      }
74  
75      @Test
76      public void testMock()
77      {
78          Assert.assertNotNull( helloWorld );
79          Assert.assertNotNull( service );
80          EasyMock.expect( service.go() ).andReturn( "Ciao" );
81          EasyMock.expectLastCall().once();
82  
83          EasyMock.replay( service );
84          helloWorld.sayHalloByServiceLists();
85          EasyMock.verify( service );
86      }
87  
88  }