NoClassDefFoundError received when testing with Mockito

263
September 11, 2021, at 07:50 AM

I have a test class:

package com.souradeep.mockito.scrapbook;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class ATest {
    @Mock
    B b;
    private A a;
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        a = new A(b);
    }
    @Test
    public void usesVoidMethod_Should_Call_Void_Method() throws Exception {
        doNothing().when(b).voidMethod();
        assertEquals(1, a.usesVoidMethod());
        verify(b).voidMethod();
    }
    @Test(expected = RuntimeException.class)
    public void usesVoidMethod_Should_Throw_Runtime_Exception() throws Exception {
        doThrow(Exception.class).when(b).voidMethod();
        a.usesVoidMethod();
    }
    @Test(expected = RuntimeException.class)
    public void testConsecutiveCalls() throws Exception {
        doNothing().doThrow(Exception.class).when(b).voidMethod();
        a.usesVoidMethod();
        verify(b).voidMethod();
        a.usesVoidMethod();
    }
}

Here I am mocking class B (please see code below)

package com.souradeep.mockito.scrapbook;
public class B {
    public void voidMethod() throws Exception {
        
    }
}

I am implementing class A (Please see code below)

package com.souradeep.mockito.scrapbook;
public class A {
    private B b;
    public A(B b) {
        this.b = b;
    }
    public int usesVoidMethod() {
        try {
            b.voidMethod();
        } catch (Exception e) {
            new RuntimeException(e);
        }
        return 1;
    }
}

When trying to run JUnit test I get this error:

java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.creation.jmock.ClassImposterizer$3
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:30)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
at com.souradeep.mockito.scrapbook.ATest.setup(ATest.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

The error is coming at the line

MockitoAnnotations.initMocks(this);

Please assist as to what could be the reason for this.

Answer 1

Add @RunWith(MockitoJUnitRunner.class)

Rent Charter Buses Company
READ ALSO
Right to Left Icicle Tree

Right to Left Icicle Tree

How can I mirror the d3 tree? I have use these code https://observablehqcom/@d3/zoomable-icicle but I can't put the father at right

63
How to reset row indexes on filtered values in React-Table?

How to reset row indexes on filtered values in React-Table?

I'm trying to implement a feature in my table where I need the current row index after a filter is applied in React-Table

126
PHP Page Navigation using arrays and foreach, trouble with category level = active

PHP Page Navigation using arrays and foreach, trouble with category level = active

So, in another post, CBroe was ever so helpful, along with @MeathCoder to illustrate the needed changesI've since been able to do the whole thing and it looks amazing, but I'm havign trouble setting the category to active! Below is my code and not sure how I'd change...

99
Django Model - Mix model fields and instance attributes in class

Django Model - Mix model fields and instance attributes in class

Not sure if I am on a completely wrong track, but I want some kind of a temporary runtime variable/field in my Django modelCurrently I have this:

108