SpringBoot Test测试类的使用
springBoot测试类的使用;解决Unable to find a @SpringBootConfiguration, you need to use @ContextConfigura错误
·
使用SpringBoot 测试类可在不需要启动程序时,即可使用。当你运行你的测试方法时他会自己启动程序调用所需使用到的mapper,service接口,实现方法。故而可在测试类中像编写正常service方法一样编写代码
pom依赖
<!--Spring Boot 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
编写测试类
import org.junit.jupiter.api.Test;
import org.sdyspx.stateartexam.manage.Application;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest(classes = Application.class)
public class MainTest {
@Resource
XXXService xxxService;
@Test
public void testDemo() {
// ...
}
}
-
如果注解@SpringBootTest(classes = 启动类名称.class)中配置了项目启动类,则该测试类可以放在test.java下任何包中
-
如果注解@SpringBootTest没有配置里面的参数classes = Application.class,则需要确保test.java下的测试类包与启动类所在的包一致,即在test.java下也需要创建com.xunan.demo包,并将测试类放在该包下。
如果不这么弄当跑测试方法时会爆 Unable to find a @SpringBootConfiguration, you need to use @ContextConfigura错误
更多推荐
已为社区贡献9条内容
所有评论(0)