![](/img/trans.png)
[英]TestNG Parameterized Test and PowerMockito causes MethodMatcherException
[英]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。
關於如何進行這項工作的任何建議?
兩年后編輯 :我現在已經從該項目退休了。 兩年后,使用不同的堆棧(Grails + GEB和SPOCK),模擬靜態數據仍然很困難,尤其是在集成測試中,您可能根本不模擬任何東西。 反正我不再這樣做了。 相反,我將Date
(或LocalDate
)作為參數(傳遞給服務方法),或者只是將測試數據集構建為相對於now
。
只是一個瘋狂的猜測,但是我看不到使用@RunWith(PowerMockRunner.class)
任何注釋應該有所幫助。 另外,請確保您使用的是PowerMock的最新版本,在撰寫本文時為: 1.6.6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.