不是Tomcat 7上运行的Web应用程序内运行之前的代码,我的部署描述符包含2个监听器,自定义的其中一个我创建和Spring的另一个问题:Spring上下文初始化
<listener>
<listener-class>com.company.appName.web.context.MyContextListener</listener-class>
</listener>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
正如我跑我的集成测试,我的侦听器根本不会被调用,所以为了克服它,我正在对自己进行一些初始化(调用一些静态方法,基本上是从我的这个侦听器调用的)。无论如何,我认为我在这里错过了一些东西,听众什么时候被叫到?为什么在我的集成测试中不会初始化?更具体地讲,Spring上下文被弄初始化那是因为我声明了它在我的测试类的顶部:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
所以从来没有实际使用的web.xml中..
在这种情况下, ,Spring的上下文总是被首先调用,在它被初始化之前我没有做任何事情的机会 - 是这样吗?有没有办法在spring的上下文之前运行一些代码?
更新: 我也想提一提,我使用的是@BeforeClass标注在我的测试套件:
@RunWith(Categories.class)
@IncludeCategory(HttpTest.class)
@SuiteClasses({ <MY TEST CLASSES> })
public class HttpSuiteITCase {
/**
* Run once before any of the test methods.
*/
@BeforeClass
public static void setTestsConfigurations() {
TestConfiguration.setup(false);
}
}
使用这种方法不能解决问题,测试类,以及我所有的春豆首先得到初始化。
在此先感谢
我曾尝试它,昨天并没有出于某种原因(我想从我的套件,因此运行的@BeforeClass方法将只运行一次。今天的作品..我昨天太困了:) – forhas 2012-07-23 07:20:46