Mockito

1. Add the ‘mockito-all’ dependency to your build.gradle file

1
2
3
4
5
6
7
8
9
10
11

dependencies {

...

testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'

...

}

Please add the above dependency in your build.gradle code.

2. Make the sample VO Object.

We need a sample VO Object for knowing how does the Mockito Test is configured. so We will make the PersonVO like the below code.

1
2
3
4
5
6
7
8

@Data
public class PersonVO {
String name;
int age;
String major;
}

3. @Mock

You can create the new object using this annotation. It’s really similar with the ‘autowired’ annotation in Spring but Mock annotation is only able to use in Test case.

1
2
3
4
5
6
7
8
9
10

import org.mockito.Mock;

public class PersonVOTest {

@Mock
PersonVO;

}

at the above code, You can see the code creating the PersonVO object using @Mock annotation. Actually You need a one more code for creating the object using @Mock. It’s like the below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import org.mockito.Mock;

public class PersonVOTest {

@Mock
PersonVO;

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

}

When you use the @Mock annotation, you should add ‘MockitoAnnotations.initMock(this)’ code in your test case. It’s like the constructor in Java.

4. @InjectMocks

For understanding the @InjectMocks Let’s define new class named ‘PersonService’ like the below.

1
2
3
4
5
6
7
8

public class PersonService {

@Autowired
PersonVO personVO;

}

If you type the code like the below then you will face a problem what you can’t control the PersonVO object in the PersonService.

1
2
3
4
5
6
7
8
9
10

import org.mockito.Mock;

public class PersonVOTest {

@Mock
PersonService personService;

}

in this case, if you use the @InjectMocks annotation, then you are able to solve this problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

import org.mockito.Mock;

public class PersonVOTest() {

@Mock
PersonVO personVO;

@InjectMocks
PersonService personService;

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

}

5. when().thenReturn()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import org.mockito.Mock;

public class PersonVOTest() {

@Mock
PersonVO personVO;

@InjectMocks
PersonService personService;

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

@Test
public void example() {
when(personVO.getName()).thenReturn("Mark");
// when you call the 'personVO.getName()' function then please return the 'Mark' String.

when(personVO.getAge()).thenReturn(20);
// when you call the 'personVO.getAge()' function then return '20'.

when(personVO.getMajor()).thenReturn("Computer Engineering");
// when you call the 'personVO.getMajor()' then return 'Computer Engineering'.

assertTrue(personVO.getName().equals("Mark")) // True
assertTrue(personVO.getAge() == 20) // True
assertTrue(personVO.getMajor().equals("Computer Engineering")) // True

assertTrue(personVO.getName().equals("Chris")) // False
assertTrue(personVO.getAge() == 30) // False
assertTrue(personVO.getMajor().equals("Electric Engineering")) // False
}
}

6. doThrow()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

import org.mockito.Mock;

public class PersonVOTest() {

@Mock
PersonVO personVO;

@InjectMocks
PersonService personService;

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

@Test(expected = IllegalArgumentException.class)
public void example() {
doThrow(new IllegalArgumentException()).when(p).setName(eq("JDM"));
// eq...
personVO.setName("JDM");
}
}

7. doNothing()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

import org.mockito.Mock;

public class PersonVOTest() {

@Mock
PersonVO personVO;

@InjectMocks
PersonService personService;

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

@Test
public void example(){
doNothing().when(personVO).setAge(anyInt());
verify(personVO).setAge(anyInt());
}
}

Share