1. 若是springBoot 工程有 启动类,java
class Application 是该工程的启动类,同时要肯定该启动类扫描的包的路径
则spring
在测试类上添加注解:app
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
2.SpringBoot没有启动类
2.1 添加 TestConfig.class
@Configuration @ComponentScan("com.**.**") @PropertySource("classpath:application.properties") public class TestConfig { }
而后编写测试类测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={TestConfig.class}) public class ServiceTest{ }