mardi 28 juillet 2015

PowerMockito.whenNew isn't working

I've got a class:

package test;

public class ClassXYZ {
    private final String message;

    public ClassXYZ() {
        this.message = "";
    }

    public ClassXYZ(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "ClassXYZ{" + message + "}";
    }
}

and a test:

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class MockClassXYZ {

    @Test
    public void test() throws Exception {
        PowerMockito.whenNew(ClassXYZ.class).withNoArguments().thenReturn(new ClassXYZ("XYZ"));

        System.out.println(new ClassXYZ());
    }
}

but it still creates a real class and prints:

ClassXYZ{}

What am I doing wrong?

P.S. Maven deps:

<dependencies>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.6</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5.6</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Aucun commentaire:

Enregistrer un commentaire