Gangmax Blog

Mockito

Mockito is a mocking framework for unit tests in Java.

Here is a code example how to use it. More details can be found in the official document.

Code to be tested

User.java
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
38
39
40
41
42
43
44
45
package code.test.mockito;

/**
* @author max
* @date 2020/7/13
* @time 16:28
*/
public class User {
private String userName;
private String email;
private String passwordMd5CheckSum;

public User() {
}

public User(String userName, String email, String passwordMd5CheckSum) {
this.userName = userName;
this.email = email;
this.passwordMd5CheckSum = passwordMd5CheckSum;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPasswordMd5CheckSum() {
return passwordMd5CheckSum;
}

public void setPasswordMd5CheckSum(String passwordMd5CheckSum) {
this.passwordMd5CheckSum = passwordMd5CheckSum;
}
}
LoginService.java
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
package code.test.mockito;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author max
* @date 2020/7/13
* @time 16:27
*/
public class LoginService {

private static List<User> existingUsers = new ArrayList<>();

static {
existingUsers.add(new User("max", "max@mocki.to",
"5f4dcc3b5aa765d61d8327deb882cf99"));
}

public User login(String userName, String password) {
for (User user : LoginService.existingUsers) {
if (user.getUserName().equals(userName) &&
DigestUtils.md5Hex(password).endsWith(user.getPasswordMd5CheckSum())) {
return user;
}
}
return null;
}
}

Unit test code

MockitoTest.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package code.test.mockito;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
* @author max
* @date 2020/7/13
* @time 15:46
*/
public class MockitoTest {

@Mock()
private LoginService loginService;

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

@Test
public void testLoginOne() {
// Create a stubbing and verify the expected behavior.
String mockedUserName = "max";
String mockedPassword = "???";
String mockedEmail = "mocked@mocki.to";
String mockedPasswordMd5Checksum = "This is a mocked user...";
Mockito.when(this.loginService.login(mockedUserName, mockedPassword)).thenReturn(
new User(mockedUserName, mockedEmail, mockedPasswordMd5Checksum));
User user = this.loginService.login(mockedUserName, mockedPassword);
Assert.assertEquals(user.getUserName(), mockedUserName);
Assert.assertEquals(user.getEmail(), mockedEmail);
Assert.assertEquals(user.getPasswordMd5CheckSum(), mockedPasswordMd5Checksum);
}

@Test
public void testLoginTwo() {
User user = this.loginService.login("max", "password");
// The mocked "loginService" instance returns null by default(when there's no stubbing).
Assert.assertNull(user);
}

@Test
public void testLoginThree() {
LoginService localLoginService = new LoginService();
User user = localLoginService.login("max", "password");
// A non-mocked "loginService" instance can still work as before, which means
// the "@Mock" annotation only affects the class instance, not the class.
Assert.assertNotNull(user);
}
}

Comments