针对springboot的测试类,2.2版本之前和之后是不一样的。
2.2版本之后
导包pom.xml
添加test依赖
<!-- starter-test:junit + spring-test + mockito --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>
注解
- @springboottest—import org.springframework.boot.test.context.springboottest;
- @test—import org.junit.jupiter.api.test;
测试
import org.junit.jupiter.api.test; import org.springframework.boot.test.context.springboottest; /** * @author wangkanglu * @version 1.0 * @description * @date 2024-07-07 11:32 */ @springboottest public class testmain { @test public void test1(){ system.out.println("-----"); } }
2.2版本之前
导包pom.xml
添加test依赖
<!-- starter-test:junit + spring-test + mockito --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>
注解
- @springboottest—import org.springframework.boot.test.context.springboottest;
- @runwith(springrunner.class)—import org.junit.runner.runwith;
- @test—import org.junit.test;
测试
import org.junit.test; import org.junit.runner.runwith; import org.springframework.boot.test.context.springboottest; /** * @author wangkanglu * @version 1.0 * @description * @date 2024-07-07 11:32 */ @springboottest @runwith(springrunner.class) public class testmain { @test public void test1(){ system.out.println("-----"); } }
注意包路径需要一致
注意测试类的包名和启动类的包名一定要一致,否则扫描不到bean对象会报空异常,如下图:
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论