Search code examples
javamockingjmockit

Unable to run JMockit tests after upgrading to 1.43 version


I have my own project that used to work perfectly with Java 8. The very basic testing frameworks I use are TestNG and JMockit.

Recently I upgraded my Java version to Java 9 and I noticed that JMockit test failed with a java modules access problem. Google took me to this stack overflow accepted solution module java.base does not read module java.desktop recommending upgrading JMockit to 1.34 or a later version. Following that I upgraded it to the latest version at the moment which is 1.43.

After doing this upgrade I no longer can run tests and all I am getting is a NullPointerException in the console output. My test to reproduce the issue is below:

package com.my.org;

import mockit.FullVerifications;
import mockit.Injectable;
import mockit.Tested;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class JMockitIssueTest {
    private class Delegate {
       public void doSomething() {
            System.out.println("Doing something");
        }
    }
    private class ClassUnderTest {
        private final Delegate delegate;

        private ClassUnderTest(Delegate delegate) {
            this.delegate = delegate;
        }

        public void useDelegate() {
            delegate.doSomething();
        }
    }

    @BeforeMethod
    public void setUp() throws Exception {
    }

    @Injectable
    private Delegate delegate;
    @Tested
    private ClassUnderTest classUnderTest;

    @Test
    public void itShouldUseDelegate() {
        classUnderTest.useDelegate();

        new FullVerifications() {{
            delegate.doSomething();
        }};
    }
}

Initially the setUp method was not there but after adding an empty one I got a bit more information in the console output which I am listing below:

Test ignored.

        java.lang.NullPointerException
        at mockit.integration.testng.TestNGRunnerDecorator.beforeInvocation(TestNGRunnerDecorator.java:32)
        at org.testng.internal.invokers.InvokedMethodListenerInvoker$InvokeBeforeInvocationWithoutContextStrategy.callMethod(InvokedMethodListenerInvoker.java:84)
        at org.testng.internal.invokers.InvokedMethodListenerInvoker.invokeListener(InvokedMethodListenerInvoker.java:62)
        at org.testng.internal.Invoker.runInvokedMethodListeners(Invoker.java:493)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:533)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:648)
        at org.testng.TestRunner.run(TestRunner.java:505)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
        at org.testng.SuiteRunner.run(SuiteRunner.java:364)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
        at org.testng.TestNG.runSuites(TestNG.java:1049)
        at org.testng.TestNG.run(TestNG.java:1017)
        at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
        at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

I tried multiple permutations such as using a @Mocked Delegate instance and creating the ClassUnderTest myself or even rolling back to Java 8. No luck.

I even tried to move the test to jUnit (not really a desired option as there are over 1400 test classes). At the moment I am running out of ideas but on the other hand the above use case is so basic that I really hope I am doing a stupid thing not being aware about what changed in the JMockit world since I was using 1.27 version of this mocking framework.

Thank u in advance for your options.


Solution

  • Maybe @Rogério means to look to the Release Notes, in which from version 1.42 the use of a -javaagent JVM parameter is needed.

    Try that, it's also mentioned on Running Tests part