首页 > 解决方案 > Travis CI 构建因 JUnit 5 失败

问题描述

我目前正在使用 Maven、GitHub、CodeCov 和 Travis 开发一个 Java 项目,但我的测试有问题。我正在使用 JUnit 5 进行测试,这是依赖项的 pom.xml 片段:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>

我还没有创建任何测试,我只有一个空的测试类,它的方法除了 @Test 标记之外什么都不做。这是课程:

package test.java.test;

import org.junit.jupiter.api.Test;

public class MainTests {

    @Test
    public void sampleTest() {
        // TODO
    }
}

这是我项目中的 travis.yml 文件:

language: java
after_success:
  - bash <(curl -s https://codecov.io/bash)
script: 
  - "mvn test"

在我的本地机器上运行“mvn test”并没有提供任何错误,但我的 Travis 构建因此失败:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/travis/build/M1RZ4/TFG/src/test/java/test/MainTests.java:[3,29] package org.junit.jupiter.api does not exist
[ERROR] /home/travis/build/M1RZ4/TFG/src/test/java/test/MainTests.java:[7,10] cannot find symbol
  symbol:   class Test
  location: class test.java.test.MainTests

我不习惯和 Travis 一起工作,所以我不知道自己做错了什么。

编辑:添加了完整的 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>TFG</groupId>
    <artifactId>TFG</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                    <artifactId>cobertura-maven-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <check />
                    </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    <dependency>
        <groupId>org.jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.0.19</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.0.6</version>
    </dependency>
    <dependency>
        <groupId>com.gestor</groupId>
        <artifactId>GestorProblema1maquina</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/GestorProblema1maquina.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.0</version>
    </dependency>
    </dependencies>
</project>

标签: javamaventravis-cijunit5

解决方案


我认为罪魁祸首是包装混乱。如指定: http : //maven.apache.org/ref/3.3.3//maven-model/maven.html#class_build pom.xml 的构建部分有一个sourceDirectory条目来指定源文件夹并testSourceDirectory指定测试源文件夹。

编译的输出显示您的测试被放置在源文件夹中,因此它们是使用具有编译范围而不是测试范围的依赖项进行编译的。

修理:

  • 源和测试源文件夹不得重叠。
  • 尽可能使用常规目录布局(很可能),并从 pom 中删除自定义

推荐阅读