Maven无法运行JUnit5测试。

huangapple go评论100阅读模式
英文:

Maven won't run JUnit5 tests

问题

以下是翻译好的部分:

(顺便说一句:是的,我在我的 pom 中包含了 Maven surefire 插件。)

我无法说服 Maven 运行我的 JUnit5 测试。

  • 我正在使用 Maven surefire 插件版本 2.22。
  • 我尝试将 surefire 插件降级到 2.19。
  • 我正在使用与另一个正常运行的项目相同的依赖项和插件。
  • 如果我将项目导入 Eclipse,Eclipse 可以找到并执行测试。
  • 测试的类文件,app.AppTest.class,位于目录 target\test-classes\app 中。
  • 我没有从 Maven 获取任何错误消息;它只是说“Tests run: 0, Failures: 0, Errors: 0, Skipped: 0”。

我在下面包含了我的 pom 的内容。有人可以告诉我我做错了什么吗?谢谢帮助。

<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>
  <groupId>uw.syp.java</groupId>
  <artifactId>Temp1</artifactId>
  <packaging>jar</packaging>
  <version>01</version>
  <name>Temp1</name>
  <url>http://maven.apache.org</url>

    <properties>
       <developer>JesseJW</developer>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

    <build>    
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgs>
                        <arg>-Xlint:all</arg>
                        <arg>-Xlint:-serial</arg>
                    </compilerArgs>
                </configuration>
            </plugin>    

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>          
        </plugins>
    </build>
</project>

按要求提供的源代码,包括导入:

package app;

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

import org.junit.jupiter.api.Test;

import java.net.URL;
import javax.swing.JOptionPane;

class AppTest
{
    void test()
    {
        System.out.println( "*************" );
        URL resURL  =  getClass().getClassLoader().getResource( "ResFile.txt" );
        System.out.println( resURL );
        assertNotNull( resURL );
        if ( resURL != null )
        {
            System.out.println( resURL.getPath() );
        }
    }
}
英文:

(BTW: Yes, I am including the Maven surefire plugin in my pom.)

I can't persuade Maven to run my JUnit5 tests.

  • I am using the Maven surefire plugin version 2.22
  • I tried downgrading the surefire plugin to 2.19
  • I'm using the same dependencies and plugins as another project which runs fine
  • If I import the project into eclipse, eclipse finds and executes the tests
  • The class file for the test, app.AppTest.class, is in directory target\test-classes\app
  • I don't get any error messages from Maven; it just says "Tests run: 0, Failures: 0, Errors: 0, Skipped: 0"

I am including the contents of my pom, below. Can anyone tell me what I'm doing wrong? Thanks for the help.

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;uw.syp.java&lt;/groupId&gt;
  &lt;artifactId&gt;Temp1&lt;/artifactId&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;version&gt;01&lt;/version&gt;
  &lt;name&gt;Temp1&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;

    &lt;properties&gt;
       &lt;developer&gt;JesseJW&lt;/developer&gt;
       &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
            &lt;version&gt;5.3.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
            &lt;version&gt;5.3.2&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;    
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.6.0&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.8&lt;/source&gt;
                    &lt;target&gt;1.8&lt;/target&gt;
                    &lt;compilerArgs&gt;
                        &lt;arg&gt;-Xlint:all&lt;/arg&gt;
                        &lt;arg&gt;-Xlint:-serial&lt;/arg&gt;
                    &lt;/compilerArgs&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;    

            &lt;plugin&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.22.2&lt;/version&gt;
            &lt;/plugin&gt;          
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

As requested: source code including imports:
package app;

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

import org.junit.jupiter.api.Test;

import java.net.URL;
import javax.swing.JOptionPane;

class AppTest
{
    void test()
    {
        System.out.println( &quot;*************&quot; );
        URL resURL  =  getClass().getClassLoader().getResource( &quot;ResFile.txt&quot; );
        System.out.println( resURL );
        assertNotNull( resURL );
        if ( resURL != null )
        {
            System.out.println( resURL.getPath() );
        }
    }
}

答案1

得分: 1

也许你的代码导入了错误的Test注解;org.junit.Testorg.testng.annotations.Test,...

应该导入org.junit.jupiter.api.Test

package app;

import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;

public class AppTest {
    @Test
    void test() {
        fail("Not yet implemented");
    }
}
英文:

Maybe your code imports wrong Test annotation; org.junit.Test, org.testng.annotations.Test, ...

org.junit.jupiter.api.Test should be imported.

package app;

import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;

public class AppTest {
    @Test
    void test() {
        fail(&quot;Not yet implemented&quot;);
    }
}

答案2

得分: 1

似乎您错过了测试方法的注解。只需将您的代码调整为:

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

import org.junit.jupiter.api.Test;

import java.net.URL;
import javax.swing.JOptionPane;

class AppTest
{
    @Test
    void test()
    {
        System.out.println("*************");
        URL resURL = getClass().getClassLoader().getResource("ResFile.txt");
        System.out.println(resURL);
        assertNotNull(resURL);
        if (resURL != null)
        {
            System.out.println(resURL.getPath());
        }
    }
}
英文:

It seems that you missed the annotation on your test methods. Just adapt your code to:

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

import org.junit.jupiter.api.Test;

import java.net.URL;
import javax.swing.JOptionPane;

class AppTest
{
    @Test
    void test()
    {
        System.out.println( &quot;*************&quot; );
        URL resURL  =  getClass().getClassLoader().getResource( &quot;ResFile.txt&quot; );
        System.out.println( resURL );
        assertNotNull( resURL );
        if ( resURL != null )
        {
            System.out.println( resURL.getPath() );
        }
    }
}

答案3

得分: -1

参数由Surefire插件(自2.2版本起)使用,并且值得仔细检查,以考虑项目目录结构:

testSourceDirectory:测试源代码目录,包含测试类源代码。
默认值为:${project.build.testSourceDirectory}(默认值:src/test/java)。

classesDirectory(用于测试的生成类)。
默认值为:${project.build.outputDirectory}(默认值:target/classes文件夹)。

要在pom.xml中更改默认值:
```xml
<build>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

-> 修改为:

<build>
    <testSourceDirectory>${project.basedir}/myPath</testSourceDirectory>

更多信息请参考:http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html


<details>
<summary>英文:</summary>

Parameters used by Surefire plugin (since 2.2) and worth double checking taking into account the project directory structure:

testSourceDirectory: the test source directory containing test class sources.
Default value is: ${project.build.testSourceDirectory} (default: src/test/java).

classesDirectory (for generated classes being tested).
Default value is: ${project.build.outputDirectory} (default: target/classes folder).

To change the defaults in pom.xml:

<build>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

-&gt;

<build>
<testSourceDirectory>${project.basedir}/myPath</testSourceDirectory>

http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html


</details>



huangapple
  • 本文由 发表于 2020年5月30日 10:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/62097012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定