英文:
Test class in spring dosn't work properly
问题
以下是翻译好的内容:
我想测试我的控制器类。但是我无法成功运行springBootTest类。我的项目使用spring boot编写。我们正在使用spring boot编写REST API。
当我尝试执行以下测试类时,仍然会在终端中看到以下行。
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();
    }
}
我在这里放置了来自我的项目的所有所需的测试依赖项。
<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>
测试类不起作用的原因可能是什么?
英文:
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();
    }
}
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.
答案1
得分: 2
使用Junit5和@SpringBootTest将加载完整的应用程序,我之前也遇到过相同的问题,你可以在这里找到有关该问题的详细信息,以及在这里找到答案。
解决方案是在您的测试中不使用@SpringBootTest。
您的测试类的解决方案如下。
@ExtendWith(MockitoExtension.class)
public class PrivilegesControllerTest {
    @InjectMocks
    private PrivilegesController privilegesController;
    @Test
    public void add() {
        assertThat(privilegesController).isNotNull();
    }
}
您还可以使用@ExtendWith(SpringExtension.class)代替@ExtendWith(MockitoExtension.class)。
英文:
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)
答案2
得分: 0
测试Spring Boot应用程序是否正在创建您的控制器,请使用@SpringBootTest注释,而要测试控制器的行为或责任,则最好使用@WebMvcTest注释。无需为一个类注释两个注释。
有两种情况可以测试控制器的责任。
- 运行服务器
 - 不运行服务器
 
对于第一种情况,您可以使用@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)启动带有随机端口的服务器。
对于第二种情况,使用@WebMvcTest来测试Web层。
所有测试用例都按照以下签名编写:
@Test
public void test_Name() throws Exception {
//您的测试定义
}
您还可以阅读Spring Boot的官方文档 https://spring.io/guides/gs/testing-web/
英文:
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.
- With running Server
 - 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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论