首页 > 解决方案 > 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));
    }

}

标签: spring-bootjunit

解决方案


实际上,问题出在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>

推荐阅读