Mockito + PowerMock + TestNG + Libgdx

I'm trying add the powermock library to the working project, but I'm getting errors.

How I add it:

1) AbsTest extends PowerMockTestCase

2) Build.gradle dependencies

3) In some test add @PrepareForTest({SomeClass.class}). After this step this error occured.

In build.gradle all libraries are included. Error occured in a place where initialize HeadlessApplication for use Gdx.* static vars.

All tests in project extend this class:

abstract public class AbsTest extends PowerMockTestCase {

    static {
        initGdx();
    }

    protected static void initGdx() {
        // Initialize libgdx headless for use static vars (e.g. Gdx.input.setInputProcessor)
        final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
        config.renderInterval = 1f / 60; // Likely want 1f/60 for 60 fps

        new HeadlessApplication(mock(Core.class), config); <=== ERROR LINE
    }

    @BeforeMethod(alwaysRun = true)
    public void initMethod() throws Exception {
        // Initialize mocks
        MockitoAnnotations.initMocks(this);
    }
}

Errors:

org.testng.TestNGException: An error occurred while instantiating class ru.coolone.adventure_emulation.input.InputGroupsTest: null at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:435) at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336) at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125) at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190) at org.testng.TestClass.getInstances(TestClass.java:95) at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81) at org.testng.TestClass.init(TestClass.java:73) at org.testng.TestClass.(TestClass.java:38) at org.testng.TestRunner.initMethods(TestRunner.java:389) at org.testng.TestRunner.init(TestRunner.java:271) at org.testng.TestRunner.init(TestRunner.java:241) at org.testng.TestRunner.(TestRunner.java:167) at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:663) at org.testng.SuiteRunner.init(SuiteRunner.java:260) at org.testng.SuiteRunner.(SuiteRunner.java:198) at org.testng.TestNG.createSuiteRunner(TestNG.java:1271) at org.testng.TestNG.createSuiteRunners(TestNG.java:1249) at org.testng.TestNG.runSuitesLocally(TestNG.java:1107) at org.testng.TestNG.runSuites(TestNG.java:1028) at org.testng.TestNG.run(TestNG.java:996) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127) Caused by: java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.powermock.modules.testng.internal.TestClassInstanceFactory.create(TestClassInstanceFactory.java:51) at org.powermock.modules.testng.internal.PowerMockClassloaderObjectFactory.newInstance(PowerMockClassloaderObjectFactory.java:46) at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:43) at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423) ... 21 more Caused by: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66) at com.sun.proxy.$Proxy19.isTypeMockable(Unknown Source) at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29) at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22) at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186) at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62) at org.mockito.Mockito.mock(Mockito.java:1729) at org.mockito.Mockito.mock(Mockito.java:1642) at ru.coolone.adventure_emulation.AbsTest.initGdx(AbsTest.java:37) at ru.coolone.adventure_emulation.AbsTest.(AbsTest.java:30) ... 29 more Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@7dc3712 at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:101) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:45) at org.mockito.internal.configuration.plugins.PluginRegistry.(PluginRegistry.java:18) at org.mockito.internal.configuration.plugins.Plugins.(Plugins.java:17) at org.mockito.internal.util.MockUtil.(MockUtil.java:24) ... 37 more Caused by: java.lang.ClassCastException: Cannot cast org.powermock.api.mockito.mockmaker.PowerMockMaker to org.mockito.plugins.MockMaker at java.lang.Class.cast(Class.java:3369) at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:97) ... 41 more

build.gradle:

...
testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testCompile 'org.testng:testng:6.13.1'
testCompile "org.mockito:mockito-core:2.8.9"
testCompile "org.powermock:powermock-api-mockito2:1.7.3"
testCompile "org.powermock:powermock-module-testng:1.7.3"
...

1 answer

  • answered 2018-01-11 19:38 CoolONE

    I'm fixed this. Im just move initGdx(); to overloaded beforePowerMockTestMethod() or beforePowerMockTestClass() and delete mockito annotations initialization because it execute in the parent class.

    fixed AbsTest class:

    abstract public class AbsTest extends PowerMockTestCase {
    
        protected static void initGdx() {
            // Initialize libgdx headless for use static vars (e.g. Gdx.input.setInputProcessor)
            final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
            config.renderInterval = 1f / 60; // Likely want 1f/60 for 60 fps
            new HeadlessApplication(mock(Core.class), config);
        }
    
        @BeforeMethod
        @Override
        protected void beforePowerMockTestMethod() throws Exception {
            initGdx();
            super.beforePowerMockTestMethod();
        }
    }
    

    or with overload beforePowerMockTestClass method

    abstract public class AbsTest extends PowerMockTestCase {
    
        protected static void initGdx() {
            // Initialize libgdx headless for use static vars (e.g. Gdx.input.setInputProcessor)
            final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
            config.renderInterval = 1f / 60; // Likely want 1f/60 for 60 fps
            new HeadlessApplication(mock(Core.class), config);
        }
    
        @BeforeClass
        @Override
        protected void beforePowerMockTestClass() throws Exception {
            super.beforePowerMockTestClass();
            initGdx();
        }
    }