Spring Boot超简单的测试类demo
Spring Boot结合Junit的简单测试类demo
1.引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2.编写测试类
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest (classes = LombdaMapAppliaction.class) //启动类aplication
@RunWith(SpringRunner.class)
@Slf4j
public class LombdaMap {
@Test
public void contextLoads() {
System.out.printf("1111");
}
}
方法需要是public 否则报错
单元测试,使用 @Resource 或 @Autowired注入的对象为空,解决办法 (注意@SpringBootTest和@RunWitch的写法必须有)
3.
java.lang.IllegalStateException Unable to find a @SpringBootConfiguration错误解决方案
在进行单元测试时我太懒了,随便建了个test包就进行测试,导致出现这个问题
解决方案:
将包名改为与其他分支一样的路径。就可以了!
如下图:
junit vintage 报错 java.lang.Exception: No runnable methods
@test 引错包了