一、入门
本文主要讨论springBoot + mockito 入门
1. 包引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.写业务
- 编写Service逻辑
package com.org.demo;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author dzh
* @date 2023/1/5 10:19
*/
@Component
public class SomethingService {
SomethingDao somethingDao;
public List<Object> queryAll() {
return somethingDao.queryAll();
}
public Object getById(Integer id) {
return somethingDao.getById(id);
}
public Object something(String name, String gender) {
return somethingDao.something(name, gender);
}
}
- 构造Dao代码
package com.org.demo;
import java.util.List;
public interface SomethingDao {
Object getById(Integer id);
List<Object> queryAll();
Object something(String name, String gender);
}
3.测试开始
- @InjectMocks,该注解修饰的的是我们要测试的类
- @Mock,该注解修饰的是我们要mock的类
- Mockito.when(somethingDao.getById(Mockito.anyInt())).thenReturn(result),当方法调用的时候直接返回结果
- Mockito.when(somethingDao.getById(Mockito.anyInt())).then((Answer) param -> {}),当方法调用的时候,根据条件判断返回结果
- Mockito.when(somethingDao.getById(-1)).thenReturn(“啥也不是”),如果是指定参数,返回指定结果
package com.org.demo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Objects;
@Slf4j
@RunWith(SpringRunner.class)
public class SomethingServiceTest {
@InjectMocks
SomethingService somethingService;
@Mock
SomethingDao somethingDao;
@Test
public void queryAll() {
List<Object> result = somethingService.queryAll();
log.info("queryAll result => {}", result);
}
@Test
public void queryById() {
Object result = somethingService.getById(0);
log.info("getById 0 => {}", result);
result = somethingService.getById(1);
log.info("getById 1 => {}", result);
result = somethingService.getById(2);
log.info("getById 2 => {}", result);
result = somethingService.getById(-1);
log.info("getById -1 => {}", result);
}
@Before
public void before() {
Mockito.when(somethingDao.queryAll())
.thenReturn(List.of("lucy", "lily"));
Mockito.when(somethingDao.getById(Mockito.anyInt()))
.then((Answer<String>) param -> {
Object arg0 = param.getArgument(0);
if (Objects.equals(arg0, 0)) {
return "me good";
}
if (Objects.equals(arg0, 1)) {
return "you perfect";
}
return "the good guy";
});
Mockito.when(somethingDao.getById(-1)).thenReturn("啥也不是");
}
}
二、踩坑
1.如果有多个入参不能有的any有的具体
有这么一个方法: Object something(String name, String gender);
这么mock:Mockito.when(somethingDao.something(Mockito.anyString(), “abc”)).thenReturn(“1”);
就会报这样的错:Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at com.org.demo.SomethingServiceTest.before(SomethingServiceTest.java:65)
2.如果指定了any的类型入参是null则不能触发mock而返回null
这么mock:Mockito.when(somethingDao.something(Mockito.anyString(), Mockito.anyString())).thenReturn(“lucy 女”);
这么调用:somethingService.something(null, null)
结果是null。
如果我们可以这么mock:Mockito.when(somethingDao.something(Mockito.any(), Mockito.any())).thenReturn(“lucy 女”); 那就可以触发mock