springboot junit

1 添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

2
// 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类
@SpringBootTest
// 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
public class AutoWorkflowDemoTest {

@Autowired
private RepositoryService repositoryService;

@Test
public void test(){
System.out.println(repositoryService);
}

}

3 注意一点很重要,我就是在这块卡了阵子!!!!
测试类的包名一定要和启动类的包名路径一致,不然会报错! 路径要一致!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

 






posted @ 2019-04-24 20:11  酷比的天朝纸民  阅读(278)  评论(0编辑  收藏  举报