模拟系统类时出现Mockito + PowerMock LinkageError


165

我有这样的代码片段:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Thread.class})
public class AllMeasuresDataTest {

@Before
public void setUp() throws Exception {
}

@Test
public void testGetMeasures() {
    AllMeasuresData measure = new AllMeasuresData();
    assertEquals(measure.getMeasures(), null);
    HashMap<String, Measure> map = new HashMap<String, Measure>();
    measure.setMeasures(map);
    assertEquals(measure.getMeasures(), map);
    measure.setMeasures(null);
    assertEquals(measure.getMeasures(), null);
}

@Test
public void testAllMeasuresData() throws IOException {
    ClassLoader loader = PowerMockito.mock(ClassLoader.class);
    Thread threadMock = PowerMockito.mock(Thread.class);
    Vector<URL> vec = new Vector<URL>();
    Mockito.when(loader.getResources("measure")).thenReturn(vec.elements());
    Mockito.when(threadMock.getContextClassLoader()).thenReturn(loader);
    PowerMockito.mockStatic(Thread.class);
    Mockito.when(Thread.currentThread()).thenReturn(threadMock);
        ...
    }
}

在运行此测试时,我得到:

java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:201)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:149)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.codecover.instrumentation.java.measurement.ProtocolImpl.initializeMBean(ProtocolImpl.java:247)
at org.codecover.instrumentation.java.measurement.ProtocolImpl.<init>(ProtocolImpl.java:237)
at org.codecover.instrumentation.java.measurement.ProtocolImpl.getInstance(ProtocolImpl.java:185)
at measure.CodeCoverCoverageCounter$6ya5ud0ow79ijrr1dvjrp4nxx60qhxeua02ta2fzpmb1d.<clinit>(MeasureCalculatorsHolder.java:146)
at measure.MeasureCalculatorsHolder.<clinit>(MeasureCalculatorsHolder.java:17)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at javassist.runtime.Desc.getClassObject(Desc.java:43)
at javassist.runtime.Desc.getClassType(Desc.java:152)
at javassist.runtime.Desc.getType(Desc.java:122)
at javassist.runtime.Desc.getType(Desc.java:78)
at algorithm.AllMeasuresDataTest.testGetMeasures(AllMeasuresDataTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl$PowerMockJUnit49MethodRunner.executeTestInSuper(PowerMockJUnit49RunnerDelegateImpl.java:116)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl$PowerMockJUnit49MethodRunner.executeTest(PowerMockJUnit49RunnerDelegateImpl.java:77)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.codecover.juniteclipse.runner.EclipseTestRunner.main(EclipseTestRunner.java:40)

你知道我该怎么预防吗?我也许还有另一种方法可以模拟这样的代码:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
...
Enumeration<URL> resources = classLoader.getResources(path);

您想嘲笑什么?又为什么呢
NilsH

第一个测试是getters和setters测试,我在那里叫构造函数(并且在那里发生异常)。第二个是构造函数测试。我想控制第三个代码段中包含的资源枚举。
Wojciech Reszelewski

1
首先,在我看来,您的测试与您的实现紧密相关。根据经验,这将导致脆弱的测试。最好是,在编写测试时要考虑“黑匣子”。“这段代码应该做什么”,而不是“这段代码是怎么做的”。其次,我认为您最好创建一组资源并让Java运行时处理类加载本身。
NilsH

可以创建各种资源集,因为它们在哪里测试用例?
Wojciech Reszelewski

当然。对您而言,最简单的方法可能是参数化资源的名称。然后,您可以将不同的资源名称传递到测试中。
NilsH

Answers:


407

尝试将此注释添加到您的Test类:

@PowerMockIgnore("javax.management.*")

为我工作。


2
精度*“达到您的测试等级”。简单实用的答案!
2015年

3
是否可以通过代码或配置来完成?我找不到任何方法可以做到这一点。我们有数百个测试...我无法全部调整。
Frederic Leitenberger

1
@FredericLeitenberger在下面看到我的答案
user3474985

2
您能否也解释一下此修复程序的直觉和含义?我们使用那条线给PowerMockito什么指令?
Swapnil B.

33

与此处接受的响应类似,我最终不得不排除所有与SSL相关的类:

@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})

将其添加到班级顶部可解决该错误。


5
仍然需要添加更多路径,但是您救了我的命男!@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.*", "javax.net.ssl.*","com.sun.*"})
弗朗西斯科·洛佩斯·桑乔

很高兴也了解com.sun。
杰森D

1
我需要以下内容:@PowerMockIgnore({“ javax.management。*”,“ javax.crypto。*”})
Kristof Neirynck

2
这救了我:@PowerMockIgnore({“ javax.management。*,” org.apache.http。*“,” com.amazonaws.http.conn.ssl。*“,” javax.net.ssl。*“ ,“ com.sun。*”,“ javax.xml。*”,“ javax.crypto。*”})
Fayaz Ahmed

26

类加载器冲突,请使用以下命令:@PowerMockIgnore("javax.management.*")

模拟类加载器不加载javax.*. 它有效。


使用后@PowerMockIgnore("javax.management.*"),测试类可以单独正常运行。但运行的Junit testFailed to load ApplicationContext错误。 org.apache.catalina.LifecycleException: A child container failed during start等等。
niaomingjian '17

8

这可能是一个古老的话题,但是我也遇到了这个问题。事实证明,当powermock发现同一包中有2个具有相同名称的类时(不同的依赖关系),某些Java版本无法处理powermockito。

在高于Java 7_25的任何版本中,都会出现此错误。


2
“在高于Java 7_25的任何版本中,都会出现此错误。”,这是很有信息的。
Kajal Sinha

这意味着什么:“无法处理powermockito”?除了通过注释忽略之外,还有什么方法可以处理它?

这是很久以前的事了,但是我认为我们通过确保在相同类型的程序包中没有两个具有相同名称的类来对其进行排序。当然,如果您有两个依赖的库,并且它们驻留在其中,这将很困难。我不知道此问题是否已解决。
Rens Groenveld '17

3

为了模拟系统类,请准备不是测试目标的类Thread.class。PowerMock无法进行检测,Thread.class因为它在JVM启动期间是必需的-远远早于PowerMock可以检测到。

仪表的工作方式,一旦加载了类,就无法再对其进行仪表化。

请参阅PowerMock Wiki


3

在PowerMock 1.7.0中,可以将用户定义的全局配置添加到项目的类路径中。PowerMockConfig

org/powermock/extensions/configuration.properties

只需在属性文件中添加一行,如下所示:

powermock.global-ignore=javax.management.*

这将解决项目中所有测试类的错误。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.