Junit5 测试覆盖率为 0%

时间:2021-06-11 12:49:37

标签: maven intellij-idea junit5

我在 Intellij Idea 中有一个 Maven 项目。我的 pom 包含几个依赖项。这是 pom.xml 的摘要。我尝试实现 junit5,为此我添加了 2 个依赖项。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myapp</groupId>
    <artifactId>app-sharepoint</artifactId>
    <version>2.0.0-SNAPSHOT</version>
    <name>SharePoint App</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.jupyter.version>5.7.2</junit.jupyter.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>msal4j</artifactId>
            <version>1.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph-auth</artifactId>
            <version>0.3.0</version>
        </dependency>
        ...
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupyter.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupyter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.11.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven
                defaults (may be moved to parent pom) -->
            <plugins>
                ...
                <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                    <dependencies>
                        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-surefire-provider -->
                        <dependency>
                            <groupId>org.junit.platform</groupId>
                            <artifactId>junit-platform-surefire-provider</artifactId>
                            <version>1.3.2</version>
                            <scope>test</scope>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

我尝试实现测试方法,到目前为止它有效。这是我实施的基本测试:

public class AuthenticationManagerTest {

    @Test
    void getAccessTokenByClientCredentialGrant_invalidUrlMissingSegments() throws MalformedURLException {
        authority = "https://somestring";

        Assertions.assertThrows(IllegalArgumentException.class, () ->
                    // ConfidentialClientApplication app =
                    ConfidentialClientApplication.builder(
                            clientId,
                            ClientCredentialFactory.createFromSecret(clientSecret))
                            .authority(authority)
                            .build(),
            "Authority Uri should not have empty path segments"
        );
    }
    ....
}

在尝试使用覆盖率运行它时,我注意到覆盖的代码为 0%。有什么遗漏吗?

enter image description here

我还注意到,当我运行所有测试时,我根本没有实施任何测试的 POJO 类确认了 20% 的覆盖率。

0 个答案:

没有答案