Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock objects in Junit test gives NoClassDefFoundError

When I mock objects in Junit test cases using of PowerMock and Mockito, I'm getting this exception:

java.lang.NoClassDefFoundError: org/mockito/exceptions/base/MockitoSerializationIssue
like image 826
Gowtham Murugesan Avatar asked Mar 27 '15 10:03

Gowtham Murugesan


People also ask

What is mock object in Junit?

Mock objects is a unit testing technique in which a code chunk is replaced by dummy implementations that emulate real code. This helps one to write unit tests targeting the functionality provided by the class under test.

Is it necessary to initialize mock objects?

initMocks(Object) is not necessary. Mocks are initialized before each test method.

How do you initialize a mocked object?

Mock objects can be initialized using Mockito annotation @Mock and MockitoAnnotations#initMocks(Object) . Pros: Declarative thanks to @Mock annotation.

How do you verify that mocked method is called?

Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.


3 Answers

I got this when combining PowerMock with Mockito. Fixed by using compatible version as shown here: https://github.com/powermock/powermock/wiki/Mockito#supported-versions

like image 52
Peter Avatar answered Oct 07 '22 08:10

Peter


Check version of your mockito lib. I see org.mockito.exceptions.base.MockitoSerializationIssue class in 2.0.2-beta. It is not present in 1.9.5 .

like image 28
Omkar Shetkar Avatar answered Oct 07 '22 08:10

Omkar Shetkar


You don't have to use the beta version. The latest 1.10.x should also work. I had the same problem when using 1.9.5, but changing to 1.10.19 fixed the problem.

like image 22
Jonas Pedersen Avatar answered Oct 07 '22 07:10

Jonas Pedersen