spring-boot - Spring Boot 单元测试未运行
问题描述
我已经尝试了很多解决方案来解决这个问题。但没有运气我的测试用例没有运行。
当我执行mvn clean package时,它正在运行单个测试用例。
服务层测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class OrderStatusServiceTests {
@Autowired
private OrderStatusServiceImpl orderStatusService;
@MockBean
private OrderStatusRepository orderStatusRepository;
@Before
public void initialize(){
orderStatusService = new OrderStatusServiceImpl(orderStatusRepository);
}
@Test
public void saveOrderDetail(){
OrderDetail orderDetail = getOrderInformation();
Mockito.when(orderStatusRepository.save(orderDetail)).thenReturn(orderDetail);
Assert.assertEquals(orderStatusService.addOrderDetail(orderDetail), orderDetail);
}
@Test
public void getOrderDetail(){
OrderDetail orderDetail = getOrderInformation();
Mockito.when(orderStatusRepository.findByUserId("abc123")).thenReturn(java.util.Optional.of(orderDetail));
Assert.assertEquals(orderStatusService.getOrderDetail("abc123"), java.util.Optional.of(orderDetail));
}
}
解决方案
实际上,问题出在pom文件上。
Maven 文件,同时收到错误。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
我所做的是删除了排除和 Junit 附加依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
推荐阅读
- javascript - 服务工作者在本地服务时注册,但不能在线工作
- python - 性能问题:根据未来值标记 Pandas 系列中的项目
- python - Kivy:MDIconButton 不在 Gridlayout 中居中,并且带有按钮行为的 MDLabel 无法移动
- c++ - 当我尝试在 C++ 中编译和运行我的“main.cpp”文件时,导致调用中止方法的原因是什么?
- php - 在php中删除开始日期和结束日期之间的日期数组
- c++ - 如何在 C++ 中接受用户输入作为向量的长度
- java - ObjectMapper 的 readValue 方法给出 MismatchedInputException
- sql-server - 如何将 2 个视图和一个表合并为 1 个视图?
- ruby-on-rails - ActiveRecord::StatementInvalid: PG::UndefinedTable: 错误: 关系“人”不存在
- javascript - 按钮在连续点击事件上将重复数据附加到 div