spring 中的测试 class 无法正常工作
Test class in spring dosn't work properly
我想测试我的控制器class。但是我无法 运行 springBootTest class。我的项目写在 spring 引导。我们正在使用 spring 引导编写 REST API。
当我尝试执行以下测试时 class。我仍然从终端获得以下信息。
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>
测试不工作的可能原因 Class。
使用 Junit5
和 @SpringBootTest
将加载完整的应用程序,我之前遇到过同样的问题,您可以找到有关问题的详细信息 and answer .
此问题的解决方案是在没有 @SpringBootTest
的情况下使用您的测试。
您的测试class的解决方案如下。
@ExtendWith(MockitoExtension.class)
public class PrivilegesControllerTest {
@InjectMocks
private PrivilegesController privilegesController;
@Test
public void add() {
assertThat(privilegesController).isNotNull();
}
}
您也可以使用 @ExtendWith(SpringExtension.class)
代替 @ExtendWith(MockitoExtension.class)
要测试 spring 引导应用程序正在 创建 您的控制器,请使用 @SpringBootTest
注释,并测试 行为或责任的控制器最好用@WebMvcTest
。无需将两个注释都注释为一个 class.
有两种情况可以检验控制器的责任。
- 有运行服务器
- 没有服务器
对于第一种情况,您可以使用 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
启动具有随机端口的服务器。
对于第二种情况,使用 @WebMvcTest
测试 web 层。
所有测试用例都使用以下签名编写:
@Test
public void test_Name() throws Exception {
//your test definition
}
您还可以阅读 Spring 引导的官方文档 https://spring.io/guides/gs/testing-web/
我想测试我的控制器class。但是我无法 运行 springBootTest class。我的项目写在 spring 引导。我们正在使用 spring 引导编写 REST API。 当我尝试执行以下测试时 class。我仍然从终端获得以下信息。
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>
测试不工作的可能原因 Class。
使用 Junit5
和 @SpringBootTest
将加载完整的应用程序,我之前遇到过同样的问题,您可以找到有关问题的详细信息
此问题的解决方案是在没有 @SpringBootTest
的情况下使用您的测试。
您的测试class的解决方案如下。
@ExtendWith(MockitoExtension.class)
public class PrivilegesControllerTest {
@InjectMocks
private PrivilegesController privilegesController;
@Test
public void add() {
assertThat(privilegesController).isNotNull();
}
}
您也可以使用 @ExtendWith(SpringExtension.class)
代替 @ExtendWith(MockitoExtension.class)
要测试 spring 引导应用程序正在 创建 您的控制器,请使用 @SpringBootTest
注释,并测试 行为或责任的控制器最好用@WebMvcTest
。无需将两个注释都注释为一个 class.
有两种情况可以检验控制器的责任。
- 有运行服务器
- 没有服务器
对于第一种情况,您可以使用 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
启动具有随机端口的服务器。
对于第二种情况,使用 @WebMvcTest
测试 web 层。
所有测试用例都使用以下签名编写:
@Test
public void test_Name() throws Exception {
//your test definition
}
您还可以阅读 Spring 引导的官方文档 https://spring.io/guides/gs/testing-web/