springboot
springboot测试工具
junit-vintage-engine 和 junit-jupiter-engine 有什么不同
junit-vintage-engine
是 JUnit 4 中使用的测试引擎。
junit-jupiter-engine
是 JUnit 5 中使用的测试引擎。
如果你的 Spring 项目使用的新的 Spring Boot 版本的话,你应该默认使用了 JUnit 5 的引擎,因此为了兼容性,你需要在 spring-boot-starter-test 这个 POM 引用的时候将 JUnit 4 的引擎去除掉。
如果自己写的Junit,有什么不同。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
package com.pctc;
import com.pctc.model.User;
import com.pctc.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//一定要有@RunWith
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest(classes=SpringbootJspJdbctemplateApplication.class)
@SpringBootTest
//这里一定要有public
public class SpringbootJspJdbctemplateApplicationTests {
@Autowired
private UserService userService;
@Test
public void testTest(){
System.out.println("good,idea");
}
@Test
public void userTest(){
User user=new User();
user.setName("guo1级1199");
user.setAge(28);
userService.createUser(user);
System.out.println("整合springboot 与junit结束");
}
}
这个是junit-vintage-engine 和 junit-jupiter-engine可以运行的。但是public警告要改private
区别:自己写的以前单元测试,是public。还有加@RunWith(SpringJUnit4ClassRunner.class),
否则会报空指针异常(在连接数据库的时候)。如果要改自己写单元测试,改包。加runwith,和要public修饰。