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

无法在powermock中使用@PowerMockIgnore创建restTemplate

郎飞龙
2023-03-14

我需要一个用于私有方法测试的powermock
如果我只使用@RunWith(PowerMockRunner.class)而不使用@powermockrunnerregate(SpringJUnit4ClassRunner.class),效果会很好

我需要使用@powermockrunnerregate(SpringJUnit4ClassRunner.class)来表示@autowired。但它并没有失败。

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyConfig.class}) 
@SpringBootTest
public class SpringPowerMockTest {

}

错误的细节

java.lang.IllegalAccessError: class javax.xml.parsers.FactoryFinder (in unnamed module @0x45d84a20) cannot access class jdk.xml.internal.SecuritySupport (in module java.xml) because module java.xml does not export jdk.xml.internal to unnamed module @0x45d84a20
    at javax.xml.parsers.FactoryFinder.(FactoryFinder.java:69)
    at javax.xml.parsers.SAXParserFactory.newInstance(SAXParserFactory.java:147)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:79)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:57)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:151)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
    at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
    at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
    at org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:55)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:417)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:362)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:655)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:99)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.powermock.reflect.internal.WhiteboxImpl.createInstance(WhiteboxImpl.java:1414)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeConstructor(WhiteboxImpl.java:1262)
    at org.powermock.reflect.Whitebox.invokeConstructor(Whitebox.java:497)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$1.call(DelegatingPowerMockRunner.java:101)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$1.call(DelegatingPowerMockRunner.java:97)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.java:132)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.createDelegate(DelegatingPowerMockRunner.java:96)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.(DelegatingPowerMockRunner.java:64)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:165)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:47)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:107)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.(JUnit4TestSuiteChunkerImpl.java:69)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.(AbstractCommonPowerMockRunner.java:36)
    at org.powermock.modules.junit4.PowerMockRunner.(PowerMockRunner.java:34)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
    at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:768)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)


因此我通过https://github.com/powermock/powermock/issues/864

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyConfig.class}) 
// I added this
@PowerMockIgnore({"javax.net.ssl.*", "org.slf4j.*", "javax.parsers.*", "ch.qos.logback.*", 
"jdk.xml.internal.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", 
"javax.management.*"})
@SpringBootTest
public class SpringPowerMockTest {

    @Test
    public void test() {
      //success
    }

}

它运行得很好。

因此,我开始编写测试代码

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyConfig.class}) 
@PowerMockIgnore({"javax.net.ssl.*", "org.slf4j.*", "javax.parsers.*", "ch.qos.logback.*", 
"jdk.xml.internal.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", 
"javax.management.*"})
@SpringBootTest
public class SpringPowerMockTest {

    @Test
    public void initalize() throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        .....
    }


}

但是我不能从这个错误信息开始

javax.xml.transform.TransformerFactoryConfigurationError: Provider com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl could not be instantiated: java.lang.reflect.InvocationTargetException
    at java.xml/javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.java:181)
    at java.xml/javax.xml.transform.FactoryFinder.find(FactoryFinder.java:257)
    at java.xml/javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:126)
    at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.(AbstractXmlHttpMessageConverter.java:52)
    at org.springframework.http.converter.xml.AbstractJaxb2HttpMessageConverter.(AbstractJaxb2HttpMessageConverter.java:38)
    at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.(Jaxb2RootElementHttpMessageConverter.java:64)
    at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.(AllEncompassingFormHttpMessageConverter.java:86)
    at org.springframework.web.client.RestTemplate.(RestTemplate.java:161)
    at com.nesic.mroc.elevator.SpringPowerMockTest.initalize(SpringPowerMockTest.java:26)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:149)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:141)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.java:132)
    at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.run(DelegatingPowerMockRunner.java:141)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:768)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.xml/javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.java:169)
    ... 47 more
Caused by: java.lang.IllegalAccessError: class com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl (in unnamed module @0x10d5ba8d) cannot access class jdk.xml.internal.JdkXmlUtils (in module java.xml) because module java.xml does not export jdk.xml.internal to unnamed module @0x10d5ba8d
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.(TransformerFactoryImpl.java:251)
    ... 52 more


我的pom。xml

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>2.0.0-beta.5</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>2.0.0-beta.5</version>
    </dependency>
    <dependency>
        <groupId>pl.pragmatists</groupId>
        <artifactId>JUnitParams</artifactId>
        <version>1.0.5</version>
        <scope>test</scope>
    </dependency>

我不知道为什么我不能创建restemplate。我不知道@PowerMockIgnore

共有1个答案

舒永嘉
2023-03-14

我通过在第行下添加代码解决了这个问题。但我不明白原因是什么。

com.sun.org.apache.xalan.internal.xsltc.trax.*

这是powermock方法的代码

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.powermock.api.mockito.PowerMockito.*;

import java.io.File;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.AllArgsConstructor;
import lombok.Data;


@SpringBootTest
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest(PowerMockTarget.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "javax.xml.transform.*", "org.xml.*", "javax.management.*", "javax.net.ssl.*", "com.sun.org.apache.xalan.internal.xsltc.trax.*"})
public class PowerMockTest {

    @Test
    public void mockStaticMethod() throws Exception {
        File fileMock = mock(File.class);
        when(fileMock.exists()).thenReturn(true);

        whenNew(File.class.getConstructor(String.class)).withArguments(anyString()).thenReturn(fileMock);
        
        PowerMockTarget target = new PowerMockTarget();
        assertEquals(target.isFileExist(), true);

        assertEquals("My bean message", message.getContent());
    }
}

@Data
@AllArgsConstructor
class MessageForTest{
    private long id;
    private String content;
    
}


@Data
@AllArgsConstructor
class MessageForTest{
    private long id;
    private String content;
    
}

PowerMock目标代码。

import java.io.File;

public class PowerMockTarget {

    public boolean isFileExist() {
    
        if(!new File("filename").exists()) {
          throw new RuntimeException();
        }
        return true;
    }
}
 类似资料:
  • 我还更改了zookeeper中的zoo.cfg。 和server.properties。 我看了所有的教程,并做了完全相同的方法。还有乌斯金Kafka开放式动物园管理员。 3)创建主题 .\bin\windows\kafka-topics.bat--create-zookeeper localhost:2181-replication-factor 1-partitions 1-topic hel

  • 我在Microsoft Windows7上使用的是。我想在一个变量中加载csv文件,以便稍后执行与SQL相关的操作,但无法这样做。我从这个链接引用了公认的答案,但没有用。我按照以下步骤创建对象和对象: 当我尝试时,它会说没有找到df。我尝试了从附加链接加载CSV的databricks解决方案。它下载软件包,但不加载csv文件。那么我怎样才能纠正我的问题呢?提前致谢:)

  • 我无法在Spark数据库中创建H2OContext--使用PySparkling。它给出了以下错误。 代码:从pysparkling导入* 代码:导入H2O 代码:H2OConf=H2OConf(Sark) 代码:H2OConf.set(“spark.ui.enabled”,True) 代码:h2oContext=h2oContext.getorcreate(spark,h2oConf) 错误:j

  • 我正在使用java创建一个zip文件,但我无法得到任何代码工作。我尝试了很多特定于Java7和Java8的方法,但所有的方法都显示出某种错误,是我的代码中有错误还是需要改进? 在这一行出现错误 请指导我:) 运行时出错:

  • 问题内容: 我无法在数据库(mySQL)中创建表,使用并尝试使用以下命令输入未来表的名称: 然后,在用户输入表名称之后,我尝试构造并调用该语句: 如果我尝试不输入名称就执行它(如常量字符串:“ CREATE TABLE newtable(…)”,但我需要输入名称),它将很好地工作。 问题答案: 阅读表名后,您将必须格式化字符串,例如: 然后创建像:

  • 我试着使用设置并查看了所有选项,但“OK”按钮无法让我完成这个过程。知道吗? 更新:确定按钮现在启用,但填写完所有内容后,我按下确定,我在控制台收到一个不同的错误: [2013-07-22 11:04:45-SDK Manager]错误:null] 访问问题:Android SDK AVD错误:null