首页 > 解决方案 > Maven Surefire 插件未通过测试,但 Junit 通过了测试

问题描述

package com.project1;

import io.github.cdimascio.dotenv.Dotenv;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Test;

import static com.project1.App.decode;
import static org.junit.Assert.assertEquals;

public class LicenseTest {
    Dotenv dotenv = Dotenv.load();
    String githubToken = dotenv.get("GITHUB_TOKEN");
    String logFile = dotenv.get("LOG_FILE");
    String logLevel = dotenv.get("LOG_LEVEL");
    private String [][] validRepositories = {{"nullivex", "browserify", "cloudinary", "lodash", "expressjs"},{"nodist", "browserify", "cloudinary_npm", "lodash", "express"}};

    @Test
    public void testCompatibleReadme(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./CompatibleReadme.md");
        assertEquals(license.getLicenseCompatibility(test), true);
    }

    @Test
    public void testCompatibleReadme2(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./CompatibleReadme2.md");
        assertEquals(license.getLicenseCompatibility(test), true);
    }

    @Test
    public void testCompatibleReadme3(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./CompatibleReadme3.md");
        assertEquals(license.getLicenseCompatibility(test), true);
    }

    @Test
    public void testCompatibleReadme4(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./CompatibleReadme4.md");
        assertEquals(license.getLicenseCompatibility(test), true);
    }

    @Test
    public void testIncompatibleReadme(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./IncompatibleReadme.md");
        assertEquals(license.getLicenseCompatibility(test), false);
    }

    @Test
    public void testIncompatibleReadme2(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./IncompatibleReadme2.md");
        assertEquals(license.getLicenseCompatibility(test), false);
    }

    @Test
    public void testIncompatibleReadme3(){
        System.setProperty("logFilename", logFile); //creating system property to be used inside log4j2.xml configuration file
        Configurator.setRootLevel(decode(logLevel));

        License license = new License();
        String test = license.getFileString("./IncompatibleReadme3.md");
        assertEquals(license.getLicenseCompatibility(test), false);
    }
}

当我运行这个测试套件时,我通过了所有的测试。但是,当我尝试通过在命令行上编写“mvn package”来使用 maven 构建这个项目时,前 4 个测试失败。这是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<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>com.project1</groupId>
  <artifactId>app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>app</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.eclipse.jgit</groupId>
      <artifactId>org.eclipse.jgit</artifactId>
      <version>5.13.0.202109080827-r</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.14.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.14.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20210307</version>
    </dependency>
    <dependency>
      <groupId>org.kohsuke</groupId>
      <artifactId>github-api</artifactId>
      <version>1.132</version>
    </dependency>
    <dependency>
      <groupId>io.github.cdimascio</groupId>
      <artifactId>dotenv-kotlin</artifactId>
      <version>6.2.2</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>8</source>
            <target>8</target>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.2.4</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <transformers>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <mainClass>com.project1.App</mainClass>
                  </transformer>
                </transformers>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

有人可以帮我弄清楚为什么会这样吗?我不知道 Maven 是否没有找到要测试的文件的正确路径或什么,但我似乎无法弄清楚发生了什么。如果有人能帮助我弄清楚发生了什么,我将不胜感激。

先感谢您。

标签: javamavenunit-testingjunit

解决方案


推荐阅读