Junit with Spring - TestContextManager [ERROR]允许TestExecutionListener捕获异常

for*_*has 2 junit spring integration-testing tomcat maven

在我的Spring-Maven - Tomcat web应用程序上的Hibernate-Mysql runnint中,我使用2种不同的Junit类别运行2种类型的Junit集成测试:

  1. LocalTests - 正在运行(不需要服务器),直接调用我的Web层方法(在这种情况下是运动衫).
  2. HttpTests - 模拟客户端并通过http请求调用我的Web层,需要tomcat启动并运行.

在每个测试课程上面我有:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)

我的测试套件看起来像那样(这是2中的一个,每个类别都有一个):

@RunWith(Categories.class)
@IncludeCategory(HttpTest.class)
@SuiteClasses({ ...All My Test Classes... })
public class HttpSuiteITCase {

    // Excluded: NotificationTests, ImageHttpTests

    /**
     * Run once before any of the test methods.
     */
    @BeforeClass
    public static void setTestsConfigurations() {
    TestConfiguration.setup(false);
    }
Run Code Online (Sandbox Code Playgroud)

我的testApplicationContext实际上是空的,它只包含组件扫描:

<context:component-scan base-package="com.company.app" />
Run Code Online (Sandbox Code Playgroud)

当我运行本地测试时,一切运行顺利,但是当我调用mt HTTP测试时它会崩溃:

2012-07-22 17:56:13 DefaultListableBeanFactory [INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2598a35d: defining beans [httpBrandManagerTestsProxy,httpClubTestsProxy,<HERE THERE'S A BUNCH OF SPRING BEANS>,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,contextApplicationContextProvider,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
2012-07-22 17:56:13 TestContextManager [ERROR] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@29565e9d] to prepare test instance [integrationTests.http.tests.UserLoginDataHttpTests@480d41f3]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
Run Code Online (Sandbox Code Playgroud)

我尝试了大量的东西没有用:(我的testApplicationContext在test/resources文件夹下.

我注意到异常标记了一个特定的类:UserLoginDataHttpTests.但我看不出这个类有什么特别之处,只是常规的春豆.

提前致谢!

for*_*has 6

问题出现是因为Spring上下文在我的一个侦听器之前加载,这是在web.xml中定义的.Spring正在初始化一些使用非弹簧clases的bes,这些弹簧使用我自己的监听器进行初始化.要解决这个问题,我应该确保我的监听器先运行.

  • @forhas我刚才遇到了同样的问题,你的回答激发了我的灵感.我提取了我的测试类的超类,它扩展了AbstractJUnit4SpringContextTests,但RunWith和ContextConfiguration注释仅在派生类上注释.添加这些注释后,测试通过. - (3认同)
  • 你如何让听众先跑 (2认同)
  • @forhas Laconic答案很简短,但可以解释:) (2认同)