使用@SpringBootTest注解进行单元测试
程序员文章站
2022-05-06 20:37:12
...
1、pom.xml文件中引入test包依赖,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、编写测试类 DemoApplicationTests.java,加入@SpringBootTest注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes={DemoApplication.class}) //指定启动类
class DemoApplicationTests {
@Autowired
private ListService service;
@Test
void contextLoads() {
//TODO 此处调用接口方法
service.listTest();
System.out.println("contextLoads");
}
@Test
public void testTwo(){
service.listTest();
System.out.println("test hello 2");
}
@Before
public void testBefore(){
System.out.println("before");
}
@After
public void testAfter(){
System.out.println("after");
}
}
结果