一、maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
二、单元测试编写
2.1、设置RunWith启动类为SpringRunner.class
2.2、@SpringBootTest标注是springboot的单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloTest {
@Autowired
private UserConfig userConfig;
@Autowired
private ApplicationContext context;
@Test
public void UserConfigTest() {
context.getBeanDefinitionNames();
System.out.println(userConfig);
}
}
注意:这里@SpringBootTest没有指定class,原因是当前test文件所属的包与应用app的包名一致,如下图所示:
如果package名字不一致,则需要指定具体包路径