junit test如何啟動web專案設定的ServletContextListener
阿新 • • 發佈:2018-11-26
/** * @Package Name : ${PACKAG_NAME} * @Author : [email protected] * @Creation Date : 2018年11月21日下午9:26 * @Function : todo */ public class systemExcetionListener implements TestExecutionListener { @Override public void beforeTestClass(TestContext testContext) throws Exception { ConfigUtilAdapter.addConfiguration(new SystemConfiguration()); ConfigUtilAdapter.init(); } @Override public void prepareTestInstance(TestContext testContext) throws Exception { } @Override public void beforeTestMethod(TestContext testContext) throws Exception { } @Override public void afterTestMethod(TestContext testContext) throws Exception { } @Override public void afterTestClass(TestContext testContext) throws Exception { } }
1 繼承TestExecutionListener
2 加入到對應的test配置中
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:/config/spring/local/appcontext-*.xml", // "classpath*:/config/spring/local/mvc-dispatcher-servlet.xml" }) @Transactional @ActiveProfiles("test") @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })