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{ }