Rechercher dans ce blog

dimanche 29 avril 2012

9. Unit-test and EasyMock

Now that the first functions have been implemented. It is key to verify they are working properly.

In this post, I will describe one unit test and explain how to use EasyMock to make sure that the functions are called the appropriate number of times and that the arguments are the one which are expected.

Maven

Let's start to include EasyMock and Junit in the maven configuration. In the project pom.xml, we have to add the following lines:
  
37       <dependencies> 
38           <dependency> 
39               <groupId>junit</groupId> 
40               <artifactId>junit</artifactId> 
41               <version>4.8.2</version> 
42           </dependency> 
43           <dependency> 
44               <groupId>org.easymock</groupId> 
45               <artifactId>easymock</artifactId> 
46               <version>2.0</version> 
47           </dependency> 
48       </dependencies>

Now that it is done, we can use junit and easy mock in our modules.

Unit test sample

Here is a simple example showing how to verify that the YoctoHub object successfully refresh its content.
 
39    
40       private YoctoTemplate yoctoTemplate; 
41    
42       @Before 
43       public void setUp() throws IOException { 
44           yoctoTemplate = EasyMock.createMock(YoctoTemplate.class); 
45    
46    
47           YoctoMap content = RestYoctoMock.getMap(RestYoctoMock.MAIN_JSON); 
48           EasyMock.expect(yoctoTemplate.query("/api.json")).andReturn( 
49                   content).once(); 
50    
51           content = RestYoctoMock.getMap(RestYoctoMock.COLOR_JSON); 
52           EasyMock.expect(yoctoTemplate.query("/bySerial/YRGBLED1-01934/api.json")).andReturn( 
53                   content).anyTimes(); 
54    
55    
56       } 
57    
58    
59       @Test 
60       public void testRefresh() throws Exception { 
61    
62           EasyMock.replay(yoctoTemplate); 
63           YoctoHub hub = new YoctoHub(yoctoTemplate); 
64           hub.refresh(); 
65           hub.findAll(); 
66    
67           Collection<YoctoObject> list = hub.findAll(YoctoProduct.YOCTO_METEO); 
68           for (YoctoObject object : list) 
69               System.out.println(object); 
70    
71           EasyMock.verify(yoctoTemplate); 
72       }

First, let me describe the overall structure of the junit tests:

  • At line 42, @Before means that this function must be called before each test to make sure tho initialize the objects. There is usually a misconception that it is called only once.
  • At line 59, @Test informs the junit framework that this function must be tested. Please note that we can add parameters to the function like expectation of exception.


In this example, we introduce a few items:
  • At line 44, we initialize a mock object: that means it is an object that will share the same API but that will return the results we expect
  • At line 48: we declare that the function query with parameter "/api.json" will be called exactly once and that the return of this function will be a constant.
  • At line 62, we inform the mock object that it must be ready to be called
  • At line 71, we verify that all the objects have been called the expected number of times
What are the advantages of EasyMock against a built-in mockup object. Well, they are numerous:
  • Only the expected function are mocked up, you do not end up with thousands of empty functions that are not needed for the test
  • You can specify the exact number of times a function will be called
  • You can reuse the mockup at many place and only need to called replay to reset the object.
  • The errors are pretty well described and it is easy to understand the issue. 
As we have already configured jenkins, we should already be able to see the results of the tests in an automated way:


Aucun commentaire:

Enregistrer un commentaire