1

我正在运行一个带有一些 JUnit4 测试的项目,这些测试在 Eclipse 上使用 PowerMockRunner 没有问题,所有测试都正常。但是在 Spring Tool Suite(通过 Eclipse)上运行相同的测试,使用 PowerMockRunner 的那些测试因以下跟踪导致的初始化错误而失败:

org.objenesis.ObjenesisException:在 org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.(SunReflectionFactoryInstantiator.java:38) 的 org.objenesis.instantiator.sun.SunReflectionFactoryHelper.newConstructorForSerialization(SunReflectionFactoryHelper.java:55) 处的 java.lang.reflect.InvocationTargetException在 org.objenesis.strategy.StdInstantiatorStrategy.newInstantiatorOf(StdInstantiatorStrategy.java:58) 在 org.objenesis.ObjenesisBase.getInstantiatorOf(ObjenesisBase.java:91) 在 org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:222 ) 在 org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139) 在 org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.registerProxyframework(AbstractTestSuiteChunkerImpl.java:158) 在 org.powermock.tests.utils.impl。 AbstractTestSuiteChunkerImpl。chunkClass(AbstractTestSuiteChunkerImpl.java:181) at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.(AbstractTestSuiteChunkerImpl.java:96) at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.(AbstractTestSuiteChunkerImpl.java:89) at org .powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.(JUnit4TestSuiteChunkerImpl.java:49) 在 org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.(AbstractCommonPowerMockRunner.java:32) 在 org.powermock .modules.junit4.PowerMockRunner.(PowerMockRunner.java:34) 在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java :64) 在 java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl。newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) 在 java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)在 org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) 在 org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) 在 org.junit.runners.model.RunnerBuilder.safeRunnerForClass (RunnerBuilder.java:59) 在 org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) 在 org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 在 org.junit。 internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76) 在 org .eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770) 在 org.eclipse.jdt .internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) 原因:java.lang.reflect.InvocationTargetException在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) 在 java.base/jdk.internal.reflect。DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:564) 在 org.objenesis.instantiator.sun.SunReflectionFactoryHelper.newConstructorForSerialization(SunReflectionFactoryHelper.java:45) ... 30 更多原因:java.lang.IllegalAccessError:由 org.powermock.core.classloader.MockClassLoader @37883b97 加载的类 jdk.internal.reflect.ConstructorAccessorImpl 无法访问 jdk/internal/reflect 超类 jdk.internal.reflect.MagicAccessorImpl在 java.base/java.lang.ClassLoader.defineClass1(Native Method) 在 java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016) 在 org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader. java:238) 在 org.powermock 的 org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:182)。core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:70) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at java.base/java.lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016) at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:238) at org.powermock.core.classloader.MockClassLoader.loadModifiedClass( MockClassLoader.java:182) 在 org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:70) 在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) 在 java.base/java .lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.System$2.defineClass(System.java:2189) at java.base/jdk.internal.reflect.ClassDefiner.defineClass(ClassDefiner.java:65) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:400) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) 在 java.base /java.security.AccessController.doPrivileged(AccessController.java:312) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor (MethodAccessorGenerator.java:112) 在 java.base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory.java:466) 在 java.base/jdk.internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 还有 35 个internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:400) at java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) at java.base/java.security.AccessController.doPrivileged( AccessController.java:312) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112) 在 java .base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory.java:466) 在 java.base/jdk.internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory .newConstructorForSerialization(ReflectionFactory.java:98) ... 还有 35 个internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:400) at java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) at java.base/java.security.AccessController.doPrivileged( AccessController.java:312) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112) 在 java .base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory.java:466) 在 java.base/jdk.internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory .newConstructorForSerialization(ReflectionFactory.java:98) ... 还有 35 个在 java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) 在 java.base/java.security.AccessController.doPrivileged(AccessController.java:312) 运行(MethodAccessorGenerator.java:400) java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112) 在 java.base/jdk.internal 的 java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393)。在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java: 98) ... 35 更多在 java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) 在 java.base/java.security.AccessController.doPrivileged(AccessController.java:312) 运行(MethodAccessorGenerator.java:400) java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112) 在 java.base/jdk.internal 的 java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393)。在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java: 98) ... 35 更多MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) at java.base/java.security.AccessController.doPrivileged(AccessController.java:312) at java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393 ) 在 java.base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory.java:466) 在 java.base/jdk 的 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112)。 internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 35 更多MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) at java.base/java.security.AccessController.doPrivileged(AccessController.java:312) at java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393 ) 在 java.base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory.java:466) 在 java.base/jdk 的 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112)。 internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 35 更多reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112) 在 java.base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory. java:466) 在 java.base/jdk.internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 35 更多reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393) 在 java.base/jdk.internal.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112) 在 java.base/jdk.internal.reflect.ReflectionFactory.generateConstructor(ReflectionFactory. java:466) 在 java.base/jdk.internal.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 35 更多reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 35 更多reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:379) 在 jdk.unsupported/sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:98) ... 35 更多

我使用 PowerMock 如下:

@RunWith(PowerMockRunner.class)
@PrepareForTest(SecurityContextHolder.class) //the static class that I have to acces
public class ClassToTest {

@Mock 
SecurityContext mockSecurityContext;

//... @Before injecting the mocks to the sut

public void methodThatUsesPowerMockClass {

//... some stuff here
PowerMockito.mockStatic(SecurityContextHolder.class):

Mockito.when(SecurityContextHolder.getContext()).thenReturn(mockSecurityContext):

}

我一直在尝试使用:

@PowerMockIgnore({ "javax.management. ", "com.sun.org.apache.xerces. ", "javax.xml. ", "org.xml. ", "org.w3c.dom. ", "com. sun.org.apache.xalan. ", "javax.activation.*" })

在课程开始但问题仍然存在。

4

0 回答 0