当前位置: 首页 > 知识库问答 >
问题:

与PowerMockito TestNG的Spring集成测试导致ClassCastExcure

郑正文
2023-03-14

我试图在与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.*"}

其他资源建议使用@规则,但如果我没弄错的话,这是针对JUnit的,我正在使用TestNG。

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

  • Powermockito 1.4.9
  • Spring3.0.5
  • 测试6.0.1

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


共有1个答案

池永长
2023-03-14

这只是一个粗略的猜测,但我没有看到使用了@RunWith(PowerMockRunner.class)的任何注释,这应该会有所帮助。另外,请确保您使用的是PowerMock的最新版本,撰写本文时的版本是:1.6。6

 类似资料:
  • 我正在使用Spring引导 1.3 开发一个 spring 应用程序 我有一个类似这样的MVC请求处理程序: 这是它的集成测试 删除了一些位以留出空间,但请注意顶部的@Transactional注释 它运行和通过没有线: 但是当添加它时,它给出了这个讨厌的异常,这是一个bean验证异常,没有任何数据插入操作(Spring mvc已经处理的验证错误和BindingResault中的结果) 当我在类级

  • 我正在尝试使用mvc-test测试我的登录页面。在我加入spring Security之前我工作得很好。 我的代码是: 测试类添加了正确的注释: 是否有任何方法可以添加DelegatingProxyFilter来使用在my security-context.xml中定义的配置测试上下文以使其工作?我尝试了一些关于注入FilterProxyChain的教程,但它在我的情况下不起作用。 有人能帮我吗?

  • 我有以下测试类: 这将导致HTTP 200而不是401。我启用了组件扫描和自动配置,并在我的SecuityConfiguration类中配置了Spring Security性,如下所示: 如果我使用RestTemplate访问,那么我将获得预期的行为(HTTP 401)。 null 因此,我不希望手动添加安全过滤器链,因为我(不正确?)期望这“只是工作”由于自动配置魔术在Spring引导? 提前谢

  • 当我运行命令gradle war integrationTest时,集成测试失败,因为它在服务项目中找不到上下文侦听器。错误消息的片段如下: 对此我们非常感谢您的帮助。提前致谢

  • 主要内容:1. 项目依赖,2. Spring组件,3. TestNG + Spring在本教程中,我们将演示如何使用TestNG测试Spring的组件。 使用的工具 : TestNG 6.8.7 Spring 3.2.2.RELEASE Maven 3 Eclipse IDE 1. 项目依赖 为了演示,首先创建一个名称为:TestngSpringIntegration 的 Maven 项目。 要将Spring与TestNG集成,您需要包依懒,添加以下内容: 创建文件:pom.xml

  • 我用的是Spring靴。我的数据库属性存在于ApplicationItest.properties文件中。 如何做这两个测试?我必须使用的正确注释是什么?有人能举个例子吗?