繁体   English   中英

使用PowerMockito + TestNG的Spring集成测试导致ClassCastException

[英]Spring integration test with PowerMockito + TestNG leads to ClassCastException

我正在尝试在与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)

根据此页面 ,您可以/应该使用@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.*"}

其他资源建议使用@Rule ,但是如果我没有记错的话,那是用于JUnit的,我正在使用TestNG。

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

  • powermockito 1.4.9
  • 春天3.0.5
  • ng 6.0.1

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM