Google

Jun 17, 2013

JUnit with Mockito tutorial

Unit testing is very important in development life cycle, and you can expect questions regarding mock objects and unit testing in general. This post extends the blog post relating to writing a mapper class for Apache Camel. This post writes a JUnit test with Mockito for the PersonMapper class.


Step 1: Have the relevant dependencies required to write unit tests.

 
<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.10</version>
 <scope>test</scope>
</dependency>
<dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-core</artifactId>
 <version>1.8.5</version>
 <scope>test</scope>
</dependency>


Step 2: The class that is being unit tested. The PersonMapper.java



 
package com.mycompany.app5;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.camel.Exchange;
import org.springframework.stereotype.Component;

@Component("personMapper")
public class PersonMapper
{
    
    private static final String HEADER_STATUS = "status";
    private static final String HEADER_DEST_FILE_SUFFIX = "dest_file_suffix";
    
    enum Status
    {
        accepted, rejected
    };
    
    public Object service(List<List<String>> data, Exchange exchange)
    {
        List<Map<String, String>> results = new ArrayList<Map<String, String>>();
        
        Map<String, String> headerLine = new LinkedHashMap<String, String>();
        headerLine.put("surname", "surname");
        headerLine.put("firstname", "firstname");
        headerLine.put("age", "age");
        
        results.add(headerLine);
        
        boolean accepted = true;
        
        for (List<String> line : data)
        {
            Map<String, String> resultLine = new LinkedHashMap<String, String>();
            resultLine.put("surname", line.get(1));
            resultLine.put("firstname", line.get(0));
            resultLine.put("age", line.get(2));
            results.add(resultLine);
            
            if (line.get(1) == null || line.get(2) == null)
            {
                accepted = false;
            }
            
        }
        
        if (accepted)
        {
            exchange.getIn().setHeader(HEADER_STATUS, Status.accepted.name());
        }
        else
        {
            exchange.getIn().setHeader(HEADER_STATUS, Status.rejected.name());
        }
        
        String srcFileName = (String) exchange.getIn().getHeader("CamelFileNameOnly");
        exchange.getIn().setHeader(HEADER_DEST_FILE_SUFFIX, srcFileName);
        
        return results;
        
    }
}


Step 3: The unit test itself with mock objects. The Exchange and Message objects from Apache Camel are mocked using the Mockito framework. Here is the PersonMapperTest class.

 
package com.mycompany.app5;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import junit.framework.Assert;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

public class PersonMapperTest
{
    
    @Mock
    Exchange mockExchange;
    
    @Mock
    Message mockMessage;
    
    PersonMapper personMapper;
    
    @Before
    public void setup()
    {
        mockExchange = mock(Exchange.class);
        mockMessage = mock(Message.class);
        personMapper = new PersonMapper();
        
        when(mockExchange.getIn()).thenReturn(mockMessage);
        //invoking a void method
        doNothing().when(mockMessage).setHeader(Mockito.anyString(), Mockito.anyString());
        //mock return values
        when(mockMessage.getHeader("CamelFileNameOnly")).thenReturn("capitalexpenses_20130510.csv");
        
    }
    
    @Test
    public void testService()
    {
        @SuppressWarnings("unchecked")
        List<LinkedHashMap<String, String>> result = (List<LinkedHashMap<String, String>>) personMapper.service(
                getPersonRecords(), mockExchange);
        
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
        LinkedHashMap<String, String> headerRec = result.get(0);
        LinkedHashMap<String, String> record = result.get(1);
        String headerStr = headerRec.values().toString();
        String recordStr = record.values().toString();
        
        Assert.assertEquals(
                "[surname, firstname, age]",
                headerStr
                );
        
        Assert.assertEquals(
                "[Smith, John, 35]",
                recordStr
                );
    }
    
    private List<List<String>> getPersonRecords()
    {
        List<List<String>> values = new ArrayList<List<String>>(5);
        
        List<String> asList = Arrays.asList(new String[]
        {
            "John", "Smith", "35"
        });
        values.add(asList);
        return values;
    }
}

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home