网上五花八门,写个实测过的方案
依赖导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class Test {
@Autowired
OqcJudgeRecordService oqcJudgeRecordService;
@org.junit.Test
public void test1() {
oqcJudgeRecordService.saveJudgeInfo(new Oqc());
System.out.println("this is test1");
}
}
代码很简单,别人写勾八一堆,最后还跑不起来,就离谱。
如果不需要实例注入,连容器都不用启动,也就是省略掉@SpringbootTest 和 @Runwith注解。