[英]Spring Boot ComandLinerRunner Test
我有個問題
@SpringBootApplication(scanBasePackages = { "com.Sample.smartbuy" })
public class SmartBuyArtApi2ClientApplication implements CommandLineRunner {
public static final Logger logger = Logger.getLogger(SmartBuyArtApi2ClientApplication.class);
public static void main(String[] args) {
logger.info("Entered the main app");
SpringApplication.run(SmartBuyArtApi2ClientApplication.class, args);
}
@Autowired
public SmartBuyController cmartBuyController;
@Override
public void run(String... args) throws SQLException {
logger.info(" ******** Applications has started ******* ");
logger.info(" cmartBuyController :: " + cmartBuyController.helloTest());
logger.info(
" ***************** cmartBuyController is not null ************* Arguments length :: " + args.length);
}
}
@Configuration
@PropertySource(ignoreResourceNotFound = true, value = "classpath:smartbuy.properties")
public class SmartBuyController {
@Autowired
private Environment env;
public String helloTest() {
return "Hello World";
}
}
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
@DataJpaTest
@SpringBootTest
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class SmartBuyControllerTest {
@MockBean
private SmartBuyController cmartBuyControllerTest;
@Autowired
ApplicationContext ctx;
@Test
public void postEntityTest() throws Exception {
CommandLineRunner runner = ctx.getBean(SmartBuyArtApi2ClientApplication.class);
runner.run();
}
}
如果您仔細查看主要功能,則cmartBuyController.helloTest()應該打印“ Hello World”,但我得到的是null,而且主應用程序還在命令行運行程序中兩次打印了日志。 請任何人能幫助您如何執行這些方法。
像這樣測試它,它對我有用;
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
@SpringBootTest
class TestClass{
@Autowired
ApplicationContext ctx;
@Test
public void testRunMethod() {
CommandLineRunner runner = ctx.getBean(CommandLineRunner.class);
runner.run();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.