constructor - PowerMock + Mockito 模拟构造函数不起作用

标签 constructor mockito powermock powermockito maven-surefire-plugin

我在运行一个简单的测试时遇到了麻烦,根据互联网上找到的所有示例,该测试应该可以正常工作。

我有一个包含以下 pom.xml 的 Java 8 Maven 项目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testingPowerMock</groupId>
    <artifactId>constructorMocking</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestingPowerMock</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.junit.platform</groupId>
                            <artifactId>junit-platform-surefire-provider</artifactId>
                            <version>1.0.3</version>
                        </dependency>
                        <dependency>
                            <groupId>org.junit.jupiter</groupId>
                            <artifactId>junit-jupiter-engine</artifactId>
                            <version>5.1.0-M1</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.1.0-RC1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.1.0-RC1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>1.6.6</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

由于我使用的是 JUnit5,因此我只能使用 SureFire 插件通过以下命令从命令行运行测试:mvn clean test

我创建了这两个非常简单的类:

public class Created {

    private String message;

    public Created(String message) {

        this.message = message;

    }

    public String getMessage() {

        return this.message;

    }

}

public class Creator {

    public Created create() {

        return new Created("Another message");

    }

}

当我运行以下测试时,它失败了。

@RunWith(PowerMockRunner.class)
@PrepareForTest(Creator.class)
public class RunnerTest {

    @Test
    public void test() throws Exception {

        Creator creator = new Creator();
        Created created = mock(Created.class);

        when(created.getMessage()).thenReturn("The expected message");

        whenNew(Created.class).withAnyArguments().thenReturn(created);

        Created aNewlyCreatedObject = creator.create();

        assertEquals("The expected message", aNewlyCreatedObject.getMessage());


    }

}

看起来构造函数调用没有被拦截,因为 Mockito 没有代理它。我尝试了不同版本的依赖项,但没有成功。我还尝试使用以下方法更改代码:

    whenNew(Created.class).withArguments("Another message").thenReturn(created);

    whenNew(Created.class).withArguments(anyString()).thenReturn(created);

但是这些方法都没有改变测试的结果。

有人可以帮我解决这个问题吗?

谢谢

最佳答案

事实证明,降级到 JUnit4 解决了问题。

下面是用于成功运行测试的最终 pom.xml。当我尝试使用 1.6.6、1.7.1 和 2.0.0-beta.5 时,更改 PowerMock 版本似乎没有任何效果。我将向 Powermock 团队发布错误。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testingPowerMock</groupId>
    <artifactId>constructorMocking</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestingPowerMock</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>2.20.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

关于constructor - PowerMock + Mockito 模拟构造函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794259/

相关文章:

c# - 通过转换 (ChildClass)parentObject 调用子构造函数;跟踪修订

javascript - 将 object.constructor 与其构造函数和 instanceof 进行比较有什么区别?

java - 模拟: how to unmock a method?

java - 如何注入(inject)java.io.File然后设置其名称

android - powermock 错误 java.lang.LinkageError : loader constraint violation: when resolving method "android. support.v4.app

android - 在 Android 上使用 PowerMockito 模拟静态方法

java - JUnit:使用构造函数而不是@Before

java - 使用 @InjectMocks 注入(inject) String 属性

java - 使用 Jacoco 对使用 Powermock 编写的测试类进行单元测试覆盖

c++ - 为什么在最后一行也没有调用复制构造函数?