1.POM 引入以下依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.2</version>
<scope>test</scope>
</dependency>
2.Service层接口单元测试示例
package xxx.xxx.xxx;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Assert;
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.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
@RunWith(MockitoJUnitRunner.class)
@Slf4j
class CommonServiceImplTest {
@Autowired
@InjectMocks
CommonServiceImpl commonService;
@Mock
ServiceA serviceA;
@Mock
ServiceB servieB;
private QueryBo queryBO;
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void getSourceCodeMap() {
queryBO = new QueryBO();
queryBO.setFieldA("A");
queryBO.setFieldB("B");
CommonEntity entity = new CommonEntity ();
entity.setField1("1");
entity.setField2("2");
Mockito.when(serviceA.list(queryBO)).thenReturn(Arrays.asList(new CommonEntity []{entity}));
Assert.assertNotNull(commonService.getSourceCodeMap());
}
CommonServiceImpl
在方法getSourceCodeMap()
调用了ServiceA
的方法list(QueryBO queryBo)
。
3. 常见问题
3.1 MockitoException
org.mockito.exceptions.base.MockitoException:
No tests found in ClientSyncServiceImplTest
Is the method annotated with @Test?
Is the method public?
解决方案:
Test引入org.junit.Test
不要引入org.junit.jupiter.api.Test
3.2 ThreadLocal类对象Mock
例如获取当前用户
@Component
public class CurrentUtil {
public CurrentUtil() {
}
public static Optional<User> getUser() {
return Optional.ofNullable(((CommonAuditService)SpringContextUtil.getBean(CommonAuditService.class)).getUser(new String[0]));
}
}
mock示例:
public XXXTest{
// ....
@Before
public void setUp(){
MockitoAnnotations.openMocks(this);
MockedStatic<CurrentUtil> userUtil = Mockito.mockStatic(CurrentUtil.class);
userUtil.when(()->CurrentUtil.getUser()).thenReturn(1L);
}
//...
}
3.3 MybatisPlusException
com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: com.cscs.icrg.universal.rating.entity.UniMainScaleDetailEntity Not Found TableInfoCache.
at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
解决方法如下:
public XXXTest{
// ....
@Before
public void setUp(){
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(),""), XXXXEntity.class);
// ...
}
//...
}
3.3 void方法调用
使用doNothing()
@Test
public void test() {
doNothing().when(mockedService).updateXXX(anyLong(),anyString());
userService.updateName(1L,"11");
verify(mockedService, times(1)).updateXXX(1L,"dddd");
}
3.4 运行时注入
使用反射修改,例如
服务
AServiceImpl
文章来源:https://www.toymoban.com/news/detail-698928.html
public class AServiceImpl implements Service{
//....
private ClassXXX fieldPostInitBean;
@PostContruct
void init() {
fieldPostInitBean = SpringContextUtil.getBean(xxxxx);
}
//...
}
AServiceImplTest
文章来源地址https://www.toymoban.com/news/detail-698928.html
//...
public Class AServiceImplTest {
@InjectMock
AServiceImpl impl;
@Mock
ClassXXX fieldPostInitBean;
@Before
void before() {
MockitoAnnotations.initMocks(this);
Field field = IcrgWarningWorkflowServiceImpl.class.getDeclaredField("fieldPostInit");
field.setAccessible(true);
field.set(this.impl,fieldPostInitBean);
}
//....
}
到了这里,关于【SpringBoot】mockito+junit 单元测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!