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

p1iqtdky  于 2023-04-06  发布在  Spring
关注(0)|答案(4)|浏览(180)

在我的Spring-Maven--Hibernate-Mysql runnint on Tomcat web app中,我使用2个不同的Junit类别运行了2种类型的Junit集成测试:

  1. LocalTests -在进程中运行(不需要服务器),直接调用我的web层方法(在本例中为jersey)。
  2. HttpTests -模拟客户端并通过http请求调用我的web层,需要tomcat启动并运行。
    在每个测试类上面,我有:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })

我的测试套件看起来像这样(这是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);
    }

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

<context:component-scan base-package="com.company.app" />

当我运行我的本地测试时,一切都很顺利,但是当我调用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)

我尝试了大量的东西,但都不起作用:(我的testApplicationContext在test/resources文件夹下。
我注意到这个异常标记了一个特定的类:但是我看不出这个类有什么特别之处,只是一个普通的springbean。
先谢谢你了!

wztqucjr

wztqucjr1#

出现问题的原因是Spring context在我的一个listener之前加载,这个listener是在web.xml中定义的。Spring正在初始化一些使用非Spring类的bean,这些bean使用我自己的listener初始化。为了解决这个问题,我应该确保我的listener首先运行。

pieyvz9o

pieyvz9o2#

对于我来说,在VM选项中添加工作
将此添加到该测试用例的VM选项中:-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

hfsqlsce

hfsqlsce3#

除了上面提到的答案。你还应该寻找“caused by”,看看是否有任何依赖解析问题。例如,如果你测试的类是MyService.java,它使用了一个依赖MyRepository.java。那么你应该确保所有这些依赖在你的测试类中都是Mocked或Autowired的。

xytpbqjk

xytpbqjk4#

超级晚,但我刚刚得到这个错误。在我的情况下,我已经更新了一些依赖项。对我来说,解决方案是在eclipse中执行Maven -〉Update Project。

相关问题