Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error initializing mockito, everytime and for any test cases

Tags:

Pretty much spent the entire day fighting this. It doesn't matter what kind of tests/class/interface I use, I get pretty much the same exception. Here's the stackerror:

java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/MockMaker$TypeMockability;
    at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:26)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167)
    at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:58)
    at org.mockito.Mockito.mock(Mockito.java:1410)
    at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
    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 org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:246)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    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:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    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:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Infact, I don't even understand why the powermock library is involved. It does get compiled as I was trying to mock some final classes, but that was hours ago!

Here's an actual set of files I'm trying to utilize:

public class ClassA {

    private String message;

    public String getMessage(){

        message="classA";
        return message;

    }
}

public class ClassB {

    private ClassA myClass;

    public ClassB(ClassA aClass){
        this.myClass=aClass;
    }

    public ClassA getClassA(){
        return myClass;
    }
}

and the testing class:

package com.icidigital.services

import com.icidigital.users.ClassA
import com.icidigital.users.ClassB
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.runners.MockitoJUnitRunner
/**
 * Created by apil.tamang on 7/28/15.
 */
@RunWith(MockitoJUnitRunner.class)
class TestSimplestMock {

    ClassB classB;

    @Mock
    ClassA mockA;

    @Before
    public void setup(){


        classB=new ClassB(mockA);

        Mockito.when(classB.getClassA()).thenReturn(null);

    }

    @Test
    public void testA(){

        ClassA obj=classB.getClassA();

        assert(obj==null);

    }
}
like image 879
apil.tamang Avatar asked Jul 28 '15 19:07

apil.tamang


People also ask

Which method in the Mockito API should be called to initialize the mocks?

For the mocks initialization, using the runner or the MockitoAnnotations. initMocks are strictly equivalent solutions. From the javadoc of the MockitoJUnitRunner : JUnit 4.5 runner initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.

How do you initialize Mockito mocks?

Mock objects can be initialized using Mockito annotation @Mock and MockitoAnnotations#initMocks(Object) .

What is MockitoAnnotations initMocks this );?

MockitoAnnotations. initMocks(this); initializes fields annotated with Mockito annotations. Allows shorthand creation of objects required for testing. Minimizes repetitive mock creation code. Makes the test class more readable.


2 Answers

It is possible for third-party plugins to register custom MockMaker implementations that create classes differently to the default behavior via Mockito's SPI. As a part of the specified behavior, since Mockito 2.0, a MockMaker can decide if a class is mockable, such as final classes in the case of PowerMock. In order to integrate such a custom behavior, PowerMock registers a custom MockMaker that is then used by Mockito.

In Mockito 2.0, the API for a custom MockMaker had to be extended but PowerMock does not yet adapt the API extensions. Therefore, this new method is not yet implemented by the PowerMock MockMaker what results in an AbstractMethodError.

like image 94
Rafael Winterhalter Avatar answered Sep 19 '22 19:09

Rafael Winterhalter


I ran into the same Error. For me it helped to include

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;

instead of Mockitos mock() and spy() methods. @Mock remained the same.

like image 43
Dornathal Avatar answered Sep 20 '22 19:09

Dornathal