Search code examples
spring-bootjunit

Spring boot unit test are not running


I have tried many solution to fix this problem. But no luck My test cases are not running.

My Test directory is like this

When i did the mvn clean package, it is running single test case. Build result

Service layer test

@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));
    }

}

Solution

  • Actually, the problem was with pom file.

    Maven file while getting the error.

    <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>
    

    What i did is removed the exclusion and Junit additional dependency.

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>