0
votes

(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.

<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>

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( "*************" );
        URL resURL  =  getClass().getClassLoader().getResource( "ResFile.txt" );
        System.out.println( resURL );
        assertNotNull( resURL );
        if ( resURL != null )
        {
            System.out.println( resURL.getPath() );
        }
    }
}
3
Show your test code including imports.johanneslink
@ johanneslink Not very experienced at this. I submitted an edit and got "It is only visible to you until it’s been approved by trusted community members "Jack Straub
Is this really your code? Where do you use your @Test annotation?Olimpiu POP
@Olimpiu POP Shoot. And I don't really mean "shoot." I've been troubleshooting an altogether different problem all day long, and I have a variety of Temp? directories and I have gotten them thoroughly confused. When I execute mvn test in the correct directory Maven finds the test just fine. Color me thoroughly embarrassed. Thanks for the help.Jack Straub
@johanneslink You called it. I had my directories mixed up.Jack Straub

3 Answers

1
votes

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( "*************" );
        URL resURL  =  getClass().getClassLoader().getResource( "ResFile.txt" );
        System.out.println( resURL );
        assertNotNull( resURL );
        if ( resURL != null )
        {
            System.out.println( resURL.getPath() );
        }
    }
}
1
votes

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("Not yet implemented");
    }
}
-1
votes

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>

->

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

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