Search code examples
dropwizardpowermockpowermockito

Making dropwizard 1.0.5 work with Powermock


How can I make Powermock work with dropwizard version 1.0.5. I have tried to include all kinds of versions of powermock to my project each time a get a different kind of error.

For example when I use:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.1</version>
    <scope>test</scope>
</dependency>

I get:

    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)

Using version 1.5.4 gives me:

org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class org.junit.internal.runners.MethodValidator.

I have even tried to use version 1.7.3 and <artifactId>powermock-api-mockito2</artifactId>

My test class is as simple as this

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyStaticMethodClass.class)
public class TestStaticMethods {

    @Test
    public void testMyStatic() {
        PowerMockito.mockStatic(MyStaticMethodClass.class);
        Mockito.when(MyStaticMethodClass.getString()).thenReturn("Hello World");

        String result = MyStaticMethodClass.getString();

        Assert.assertEquals("Helo World", result);
    }

}

I have looked into the documentation of powermock my junit version is 4.12 https://github.com/powermock/powermock/wiki/Mockito-Maven

I have the following external libraries

Mockito

Are they fetched from

<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-testing</artifactId>
    <scope>test</scope>
</dependency>

Tried to exclude them but they don't disappear I am using Intellij as my IDE. Is it because of these libraries that there might be some conflicting initializations of the testing environment?

EDIT 1

Ok, so I have tried to create a small java project with nothing other than a the following dependencies:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.7.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.7.3</version>
    <scope>test</scope>
</dependency>

my libraries are:

enter image description here

And my test file is exactly the same as above, then it works perfectly fine. So I guess it has to do something with Dropwizard...?


Solution

  • I have created a simple project using DropWizard and PowerMock and the tests execution were successful using all different versions of PM (1.6.1, 1.7.3 and 1.5.4), both using Intellij and Maven.

    Having said that, it is strange that the dropwizard-testing artifact is pulling different versions of mockito (1.10.8 for all and 2.0.54-beta for core). You can exclude the mockito-core dependency from the dropwizard-testing artifact and that will at least ensure that there are no conflicting versions of mockito.

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-testing</artifactId>
        <version>${dropwizard.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    I have also tested with versions 1.1.7 and 1.2.4 of DW but both worked fine for me.