springboot单元测试
2022-10-12 本文已影响0人
煮茶听雨
单元测试步骤
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 添加测试父类
指定启动类,端口随机
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WksixApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WksixApplicationTests {
@Test
public void contextLoads() {
}
}
- 测试具体service,只要继承上面的测试父类即可
public class ArticleServiceImplTest extends WksixApplicationTests {
@Autowired
ArticleService articleService;
@Test
public void test(){
articleService.testTransaction();
}
}
@Test 导入的是 org.junit.Test