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



我遇到了一个简单测试的麻烦,因此,在Internet中发现的所有示例都应无用任何问题。

我有一个Java 8 Maven项目,带有以下pom.xml:

<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下方用于成功运行测试的最终pom.xml。更改PowerMock版本似乎没有任何效果,因为我尝试使用1.6.6、1.7.1和2.0.0-Beta.5。我将向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>

显然不支持junit5(尚未?(。这是添加Junit5支持的史诗。https://github.com/powermock/powermock/issues/830。太糟糕了,他们没有修复PowerMock的资源,因为它确实是一个非常有力的框架,可以为旧应用程序进行单元测试。

我认为,当您需要PowerMock进行单元测试时,您尝试测试的课程未正确编写,应重构以进行测试。仅对于旧版代码,这有时太多了,因此PowerMock使您能够测试原本无法测试的类。

相关内容

  • 没有找到相关文章

最新更新