NoClassDefFoundError - org/mockito/exceptions/Reporter when Using Mockito and PowerMock

时间:2019-03-19 15:03:00

标签: java mockito powermock

I'm trying to add Powermock to my application that uses Mockito. This is what I currently have in my gradle build:

 compile("junit:junit:4.12"){force = true}
  compile("org.powermock:powermock-api-mockito-common:1.7.0")     
  compile("org.powermock:powermock-api-mockito2:1.7.0")
  compile("org.powermock:powermock-api-support:1.7.0")
  compile("org.powermock:powermock-core:1.7.0")
  compile("org.powermock:powermock-module-junit4:1.7.0")
  compile("org.powermock:powermock-module-junit4-common:1.7.0")
  compile("org.powermock:powermock-reflect:1.7.0")
  compile("org.javassist:javassist:3.12.0.GA")
  compile("org.mockito:mockito-core:2.8.9")

This is the test file that I have so far...

@RunWith(PowerMockRunner.class)
@PrepareForTest(XMLTransaction.class)
public class CloseSummaryOrCloseTrailerResponseTest {

     public final static String URL="WL_APPSERVER";
     private XMLTransaction xmlTransaction; 

    @Before
    public void initMocks() throws Exception {
        xmlTransaction = PowerMockito.spy(new XMLTransaction(URL));
        PowerMockito.doNothing().when(xmlTransaction, "initialize");    
    }

    @Test
    public void whenCloseSummaryResponseNoErrorExpectCorrectXmlMsgProduced ()
    {
        ...test code here
    }
}

I get the error:

java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter at sun.reflect.GeneratedSerializationConstructorAccessor8.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48) at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:251) at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)

The Reporter class should be contained in mockito-core. What additional jars do I need? I reviewed the compatiability of Mockito and Powermock using this site: https://github.com/powermock/powermock/wiki/Mockito#supported-versions

But it seems I am missing some other jar.

UPDATE Checked that answer but it did not solve my problem. I changed

compile("org.powermock:powermock-api-mockito2:1.7.0")

to

compile("org.powermock:powermock-api-mockito2:1.7.0RC2")

Now I get a different error:

java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath. at org.powermock.reflect.proxyframework.ProxyFrameworkHelper.register(ProxyFrameworkHelper.java:35)

What jar does this need?

1 个答案:

答案 0 :(得分:0)

对我有用的是...删除了lib文件夹中的所有powermock jar,然后使用上面的gradle代码进行了重建。 我能够进行测试。