Why do I get NoClassDefFoundError when running JMo

2020-04-18 05:21发布

问题:

I am running JMockit with Junit 4 and get the error java.lang.NoClassDefFoundError.

I want to test how my class (mycomponent) deals with values being returned from a 3rd party library static class. I am approaching this by creating the mocked class (mocked3rdpartycomponent) and then calling my mycomponent as below:

mytest->mycomponent->mocked3rdpartycomponent

The mocked class has the definition as follows. The mocked method and class is to substitute the before mentioned 3rd party library:

new Mockup<MockedClass>() {
  @Mock public ReturnType[] mockedMethod() {
    ReturnType[] ni = {null};
    return ni;
  }
}

Mycomponent mc = new MyComponent();

When I run my test class I get the following error:

java.lang.NoClassDefFoundError: mockit/internal/state/TestRun
at <MockedClass.mockedMethod>(MockedClass.java)
at <MyComponent>.<init>
caused by: java.lang.ClassNotFoundException: mockit.internal.state.TestRun

Edit: In addition to this, if I try to step over the call in the Unit Under Test which should call my fake, a screen appears which states that source code for Junit cannot be found.

Edit: The third party library is also native with some static methods.

Does anybody know what this could be caused by?

My environment is Eclipse Mars 4.5.0, JUnit 4.10, JMockit 1.19.

Thanks!