使用PowerMockito + TestNG进行Spring集成测试会导致ClassCastException

时间:2015-03-18 11:04:39

标签: java spring integration-testing testng

我试图在 TestNG 的集成测试中使用 PowerMockito 模拟一个静态方法,但到目前为止还没有快乐。

@ContextConfiguration(locations = {"classpath:applicationContextTest.xml"})
@DatabaseSetup("/my/project/dataset.xml")
@PrepareForTest({Calendars.class})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class SomeIntegrationTest {

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @BeforeMethod
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testOne() {
        // Mock date now
        Calendar now = Calendar.getInstance();
        now.setTime(DateUtil.getDate(2014, Calendar.DECEMBER, 17));
        Calendars.CalendarMutator nowCalMut = Calendars.mutate(now);
        PowerMockito.mockStatic(Calendars.class);
        PowerMockito.spy(Calendars.class);
        PowerMockito.when(Calendars.now()).thenReturn(nowCalMut);
    }
}

此模拟机制适用于单元测试,但不适用于集成测试。我收到以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
Caused by: javax.xml.parsers.FactoryConfigurationError: Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl could not be instantiated: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
Caused by: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory at javax.xml.parsers.FactoryFinder.newInstance(FactoryFinder.java:190)

根据This page,您可以/应该使用@PowerMockIgnore注释来摆脱类加载器问题。我尝试了几种(随机)组合(当然是一种):

@PowerMockIgnore({"org.w3c.dom.*", "javax.xml.*", "org.xml.*"})

我走得更远:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [jaxws-clients.xml]
Offending resource: class path resource [applicationContextTest.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [jaxws-clients.xml]; nested exception is org.springframework.beans.FatalBeanException: Class [org.apache.cxf.jaxws.spring.NamespaceHandler] for namespace [http://cxf.apache.org/jaxws] does not implement the [org.springframework.beans.factory.xml.NamespaceHandler] interface

但这看起来像是一场自由式变化的狂野追逐:

@PowerMockIgnore({"org.w3c.*", "javax.xml.*", "org.xml.*", "org.apache.*", "org.w3c.dom.*", "org.apache.cxf.*"}

其他resource建议使用@Rule,但如果我没有弄错的话,那就是JUnit的使用,我使用的是TestNG。

关于如何使这项工作的任何建议?

  • powermockito 1.4.9
  • spring 3.0.5
  • testng 6.0.1

2年后编辑:我现在已经退出了该项目。两年后,使用不同的堆栈(Grails + GEB和SPOCK),模拟静态数据仍然很难,特别是在集成测试中,您可能根本不应该模拟任何东西。反正我也不会这样做了。相反,我将Date(或LocalDate)作为参数传递给服务方法,或者只是将测试数据集构建为相对于now

1 个答案:

答案 0 :(得分:1)

只是一个疯狂的猜测,但我没有看到@RunWith(PowerMockRunner.class)正在使用的任何注释应该有所帮助。此外,请确保您使用的是最新版本的PowerMock,在撰写本文时:1.6.6