spring-boot单元测试对weblistener的加载测试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ikevin/article/details/78530097

        使用spring-boot对web项目进行测试时对weblistener进行加载.以proxool连接池的加载为例.

//原监听器代码

       @WebListener
       public class ProxoolListener implements ServletContextListener{
       @Override
       public void contextDestroyed(ServletContextEvent arg0) {

       }
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
        
        loadProxool();
        
        }
       ...//其他实现方法
       }
         //spring-boot测试适配修改,继承TestExcutionListener接口,实现prepareTestInstance方法,将监听业务同样放在此方法中做预处理即可.

      @WebListener
      public class ProxoolListener implements ServletContextListener,TestExecutionListener{
	 @Override
	 public void contextDestroyed(ServletContextEvent arg0) {

	 }

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		loadProxool();
		
	}

	@Override
	public void afterTestClass(TestContext arg0) throws Exception {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void afterTestMethod(TestContext arg0) throws Exception {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void beforeTestClass(TestContext arg0) throws Exception {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void beforeTestMethod(TestContext arg0) throws Exception {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void prepareTestInstance(TestContext arg0) throws Exception {
                //启动测试时需要预先的处理
		loadProxool();
		
	}
}

//测试类

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(listeners = { ProxoolListener.class , DependencyInjectionTestExecutionListener.class })
public class DemoApplicationTest {
	@Test
	public void exampleTest() {
		try {
			System.out.println("Connection is closed : "+ProxoolUtility.getConnection("proxool.iovdc").isClosed());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}



猜你喜欢

转载自blog.csdn.net/ikevin/article/details/78530097