首页 > 解决方案 > 为什么 Maven 不能运行测试而 VSCode 可以?

问题描述

所以当我尝试在 Maven 上运行一些测试时,测试找不到 Junit 测试,但是当我在 VSCode 上运行时,测试正在运行。我试图在 Stack Overflow 上寻找一些解决方案,但与我的问题没有类似的目录结构:

/my_program/pom.xml
/my_program/src/main/java/GestionQuality.java
/my_program/src/test/java/GestionQualiteTest.java

GestionQualite.java :

import java.util.Map;
import java.util.*;

public class GestionQualite {
    protected Map<String, String> map = new HashMap<String, String>();
    /**
     * Return the quality of a command from 1 to 5 
     * @param command the command 
     * @return the quality of the command 
     */
    public String make_quality(String command){
        map.put("command","2");
        if (map.containsKey(command)){
            return map.get(command);
        }
        else {
            return "qualite non renseigne";
        }
    }
}

GestionQualityTest.java 包含:

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
public class GestionQualiteTest {
    /**
     * Check if the quality is empty 
     * 
     * @result an error if the quality level is empty
     * 
     */
    GestionQualite gestionqualite= new GestionQualite();
    @Test
    public void CheckQualityFailTest() {
        String command = "";
        assertEquals(gestionqualite.make_quality(command),"qualite non renseigne");
    }
    
    /**
     * Check if the quality isnt empty for a command
     * 
     * @result the quality of a command
     * 
     */
    @Test
    public void CheckQualitySucceedTest() {  
        assertEquals(gestionqualite.make_quality("command"),"2");
    }
    
}

我的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <!-- =============================================================== -->
  <!-- Informations du projet -->
  <!-- =============================================================== -->
  <!-- ===== Informations Maven ===== -->
  <groupId>oasis.goodDev</groupId>
  <artifactId>preudhomme</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
  <!-- ===== Informations générales ===== -->
  <name>Preud Homme</name>
  <description> Future ERP de la banque preudhomme</description>
  <url>http://www.exemple.org/mon-appli</url>
  
  
  
  <!-- ===== Organisation ===== -->
  <organization>
    <name>Good Oasis Dev</name>
    <url>http://www.exemple.org/</url>
  </organization>
  
  <!-- ===== Informations des dependances ===== -->
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.7.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <!-- ===== Information du build ===== -->
  <build>
    <finalName>preudhomme</finalName>
    <plugins>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.9.1</version>
        <configuration>
          <port>9000</port>
          <tempWebappDirectory>${basedir}/target/site</tempWebappDirectory>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M2</version>
        <executions>
          <execution>
            <id>enforce-no-snapshots</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireReleaseDeps>
                  <message>No Snapshots Allowed!</message>
                </requireReleaseDeps>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
          <execution>
            <id>enforce-maven</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireMavenVersion>
                  <version>[3.0.0,)</version>
                </requireMavenVersion>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
          <execution>
            <id>enforce-java</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireJavaVersion>
                  <version>[11,12)</version>
                </requireJavaVersion>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <id>print-profile</id>
            <phase>compile</phase>
            <goals>
              <goal>active-profiles</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    
  </build>
  
  <!-- ===== Informations du reporting ===== -->
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.9</version>
      </plugin>
    </plugins>
  </reporting>
  
  
  <profiles>
    
    <!-- Profil local -->
    <profile>
      <id>local</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    
    <!-- Profil pour les tests -->
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <excludes>
                <exclude>**/*IntegrationTest.java</exclude>
              </excludes>
            </configuration>
            <executions>
              <execution>
                <id>integration-test</id>
                <goals>
                  <goal>test</goal>
                </goals>
                <phase>integration-test</phase>
                <configuration>
                  <excludes>
                    <exclude>none</exclude>
                  </excludes>
                  <includes>
                    <include>**/*IntegrationTest.java</include>
                  </includes>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    
    <!-- Profil pour la production-->
    <profile>
      <id>prod</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
    </profile>
  </profiles>
  
</project>

我对 Maven 的看法

我在 VSCode 中拥有的东西

标签: javamavenvisual-studio-code

解决方案


推荐阅读