1
votes

I want to write the Unit test using PowerMockito/Mockito for my static method/void method. But When I try to run , I got the following error:

/Users/<username>/Library/Java/JavaVirtualMachines/corretto-
---- IntelliJ IDEA coverage runner ---- 
sampling ...
include patterns:
exclude patterns:
ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not a subtype
ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not a subtype
java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockHandler()Lorg/mockito/invocation/MockHandler;

    at org.powermock.api.mockito.invocation.MockHandlerAdaptor.getMockHandler(MockHandlerAdaptor.java:56)
    at org.powermock.api.mockito.invocation.MockHandlerAdaptor.createInvocation(MockHandlerAdaptor.java:81)
    at org.powermock.api.mockito.invocation.MockHandlerAdaptor.performIntercept(MockHandlerAdaptor.java:61)
    at org.powermock.api.mockito.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:93)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:186)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:168)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:138)


I am new to use powerMockito/Mockito, Can Anyone help to figure out the exact issue.

This is my CreateTaskBuilder Class method that I want to test: Here JgitAccessor.clone() is a static void methid that I used donothing() for it.

  public void Repository() throws DependencyFailureException, IOException, GitAPIException {
        try {
            ServiceAccessor.loadTmpSshTicket();
            if (!Files.exists(Paths.get(LambdaEnv.GIT_SSH_SCRIPT.getValue()))) { // getValue will throw exception on null
                throw new IllegalStateException(String.format("Environment variable GIT_SSH points to file %s but it doesn't exist.",
                        LambdaEnv.GIT_SSH_SCRIPT.getValue()));
            }
            JgitAccessor.clone(REPO_URI, CLONED_REPO_PATH);
        } catch (IOException | DependencyFailureException e) {
            log.info("Exception occurred while performing Service client integration. exception: [{}]", e.getMessage());
            e.printStackTrace();
        }
    }

And this is the unit test class :

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.mockito.Mockito.*;


@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*"})
@PrepareForTest({CreateTaskBuilder.class, LambdaEnv.class, ServiceAccessor.class, JgitAccessor.class})
public class CreateTaskBuilderTest extends TestUtils {

    @Mock
    private ServiceAccessor serviceAccessor;

    @Mock
    private JgitAccessor jgitAccessor;

    @InjectMocks
    CreateTaskBuilder builder;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void loadServiceTicket_happyCase() throws Exception {
        doNothing().when(serviceAccessor).loadTmpSshTicket();
        PowerMockito.mockStatic(System.class);
        Mockito.when(System.getenv("GIT_SSH")).thenReturn("/tmp/ssh.sh");
        PowerMockito.mockStatic(Files.class);
        Mockito.when(Files.exists(Paths.get("/tmp/ssh.sh"))).thenReturn(true);
        PowerMockito.mockStatic(JgitAccessor.class);
        PowerMockito.doNothing().when(JgitAccessor.class, "clone", Mockito.anyString(), Mockito.anyString());
        builder.cloneRepository();
    }

I am using Mockito = 2.28.x; PowerMockMockito = 2.x;

1

1 Answers

0
votes

It looks like you have the wrong versions of libraries on your classpath.

The version of PowerMock you are using requires a Mockito with an org.mockito.MockingDetails.getMockHandler() which is not available in Mocktio 2.8.x. You can find it in a later version in 2.23.x.

Looking at minimum version dependencies for powermock-api-mockito2 version 2.0.0 you should be using mockito version 2.23.0 or later.

So looks like you need to update Mockito to a later version compatible with your PowerMock version, 2.23.x or later instead of 2.8.x.