Mockito logo How do tests with Mockito.

This page is under construction.

1. Add dependency in your project Maven

<dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.0.7-beta</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
</dependency>

2. Create a test Class

In Intellij open a Class and the shortcut to create a test Class is. : CTRL + shift + t

Create test

// Example service
public class MyServiceImpl implements MyService {

    @Inject
    private MyRepositoryDAO myRepositoryDAO;

    public Foo testExample(final Integer id) throws Exception {
        ...
    }
}

// Example class test
@RunWith(MockitoJUnitRunner.class)
public class MyServiceImplTest {

    @InjectMocks
    private MyServiceImpl myService;

    @Mock
    private MyRepositoryDAO myRepositoryDAO;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testExample() throws Exception {
        ...
    }

}

3. What test?

I have a service to test with his methods. I should do what test to cover the largest number of cases?

public class MyServiceImpl implements MyService {

    @Inject
    private MyRepositoryDAO myRepositoryDAO;

    public Foo getById(final Integer id) throws Exception {

        // 1. Test if id is null : expected Exception
        if (id == null) {
            throw new Exception();
        }

        // 2. Test whether the argument is passed to the DAO.
        final Foo foo = myRepositoryDAO.getById(id);

        // 3. Test if foo is null : expected Exception
        if (foo == null) {
            throw new Exception();
        }

        // 4. Finally a test with no error.
        return foo;
    }

    public List<Foo> findByName(final String name) throws Exception {

        // 1. Test if name is null : expected Exception
        // 2. Test if name is not null but empty ("") : expected Exception
        if (name == null || name.equals("")) {
            throw new Exception();
        }

        // 3. Test whether the argument is passed to the DAO.
        final List<Foo> foo = myRepositoryDAO.findByName(name);

        // 4. Test if foo is null : excepted ArraysList empty
        if (foo == null) {
            return new ArrayList<Foo>();
        }

        // 5. Finally a test with no error.
        return foo;
    }

    public Foo updateFoo(Foo foo) throws Exception {

        // 1. Test if foo is null : expected Exception
        // 2. Test if foo is not null but if id is null : expected Exception
        if (foo == null || foo.getId() == null) {
            throw new Exception();
        }

        // 3. Test if lastModifiedDate is modified.
        foo.setLastModifiedDate(new Date());

        // 4. Finally a test with no error.
        return myRepositoryDAO.updateFoo(foo);
    }
}

GetId()

#1 Test if id is null : expected Exception

@Test(expected = Exception.class)
 public void getById_nullId() throws Exception {
     myService.getById(null);
 }

#3 Test whether the argument is passed to the DAO.

@Test(expected = Exception.class)
public void getById_daoReturnNull() throws Exception {

   Mockito.when(myRepositoryDAO.getById(Mockito.eq(1))).thenReturn(null);

   myService.getById(1);
}

#4 Finally a test with no error. And #2 test whether the argument is passed to the DAO.

@Test
public void getById() throws Exception {

   final Date date = new Date();

   final List<String> list = new ArrayList<String>();
   list.add("1");
   list.add("2");
   list.add("3");

   final Foo foo = new Foo();
   foo.setId(1);
   foo.setLastModifiedDate(date);
   foo.setName("name");
   foo.setList(list);

// Check if myRepository is called with 1 and returned my object foo
   Mockito.when(myRepositoryDAO.getById(Mockito.eq(1))).thenReturn(foo);

   final Foo result = myService.getById(1);

   Assert.assertEquals(Integer.valueOf(1), result.getId());
   Assert.assertEquals(date, result.getLastModifiedDate());
   Assert.assertEquals("name", result.getName());
   Assert.assertEquals(Integer.valueOf(3), Integer.valueOf(result.getList().size()));

   // two lists are defined to be equal if they contain the same elements in the same order.
   Assert.assertEquals(list, result.getList());
}

4. How mock private methode ?

5. How captor a property ?

6. How spy a methode ?

7. Run test with covrage in Intellij

To run the tests with Intellij, you only have to click right on the Class or the Class has an external method if you click in a way only this test method will be run.

Run test with coverage

After your test is finished, you can go into the Class or that you have tested. And look to your left. You will see green and red bars may be darker than me because I changed the configuration in Intellij gluer.

coverage


You can click the green bars to view the number of times that his line was covered by your tests.

Number hits


At the bottom right of your Intellij you your new option appear. Click above to see the percentage of power line method and hedged Class.

New icon coverage


Click in the packages you want to go and watch the number of percentage cover your project.

All coverage

8. How test DAO with Flyway ?

9. How test Controller ?

10. Best practice