Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用PowerMockito和x2B进行Spring集成测试;TestNG导致ClassCastException_Java_Spring_Integration Testing_Testng - Fatal编程技术网

Java 使用PowerMockito和x2B进行Spring集成测试;TestNG导致ClassCastException

Java 使用PowerMockito和x2B进行Spring集成测试;TestNG导致ClassCastException,java,spring,integration-testing,testng,Java,Spring,Integration Testing,Testng,我试图在与TestNG的集成测试中,用PowerMockito模拟一个静态方法,但到目前为止没有任何乐趣 @ContextConfiguration(locations = {"classpath:applicationContextTest.xml"}) @DatabaseSetup("/my/project/dataset.xml") @PrepareForTest({Calendars.class}) @Transactional @TransactionConfiguration(def

我试图在与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
注释来消除类加载器问题。我尝试了几种(随机)组合(当然,一次一种):

我更进一步:

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
  • testng 6.0.1

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

相关,这只是一个粗略的猜测,但我没有看到使用
@RunWith(PowerMockRunner.class)
的任何注释。另外,请确保您使用的是PowerMock的最新版本,在撰写本文时是:
1.6.6

我认为它位于祖先类或实际注释中的某个地方(原始代码已被清理以便发布)。请参阅我对原始帖子的更新。
@PowerMockIgnore({"org.w3c.*", "javax.xml.*", "org.xml.*", "org.apache.*", "org.w3c.dom.*", "org.apache.cxf.*"}