Jahongir  Sabirov
Jahongir Sabirov

Reputation: 470

Test class in spring dosn't work properly

I would like to test my controller class. But I couldn't manage to run springBootTest class. My project written in spring boot. We are writing REST API using spring boot. When I try to excute following test class. I still get following line from terminal.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;



/*
 *
 *  @A Sabirov Jakhongir
 *
 */


@SpringBootTest
@WebMvcTest
public class PrivilegesControllerTest {

    @Autowired
    private PrivilegesController privilegesController;


    @Test
    public void add() {
        assertThat(privilegesController).isNotNull();

    }
}

enter image description here

I put here all needed dependency for testing from my project.

    <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/org.junit.platform/junit-platform-launcher -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>

What might be cause of not working of test Class.

Upvotes: 0

Views: 780

Answers (3)

Amine Hatun Ergin
Amine Hatun Ergin

Reputation: 1

I added these dependencies on my pom.xml file,

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>

And after that, go to your test class and write it at the beginning of your page import org.junit. You will get an alert and it will say

add junit to classpath

Click it and write import org.junit.Test;. I solved my problem this way.

Upvotes: 0

Subrato Pattanaik
Subrato Pattanaik

Reputation: 6059

To test spring boot application is creating your controller, use @SpringBootTest annotation, and to test the behavior or responsibility of the controller is better to use @WebMvcTest. No need to annotate both the annotation to one class.

There are two cases to test the controller's responsibility.

  1. With running Server
  2. Without Server

For 1st Case, you can use @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) to start a server with a random port.

For 2nd Case, use @WebMvcTest for testing the web layer.

All the test cases are written with the following signature:

@Test
public void test_Name() throws Exception {
//your test definition
}

You can also read the Official Documentation of Spring Boot https://spring.io/guides/gs/testing-web/

Upvotes: 1

SSK
SSK

Reputation: 3766

With Junit5 and @SpringBootTest will load the full application, I had faced the same issue before, you can find details about the question here and answer here.

The solution for this is to use your test without @SpringBootTest. The solution to your test class is as below.

@ExtendWith(MockitoExtension.class)
public class PrivilegesControllerTest {

    @InjectMocks
    private PrivilegesController privilegesController;


    @Test
    public void add() {
        assertThat(privilegesController).isNotNull();

    }
}

You can also use @ExtendWith(SpringExtension.class) instead of @ExtendWith(MockitoExtension.class)

Upvotes: 2

Related Questions