Maven non trova i test JUnit da eseguire


Ho un programma maven, compila bene. Quando eseguo mvn test non esegue alcun test (sotto l'intestazione TEST dice There are no tests to run.).

Ho ricreato questo problema con una configurazione super semplice che includerò di seguito e l'output quando eseguito con -X.

I test unitari vengono eseguiti correttamente da eclipse (sia con il suo pacchetto junit predefinito che quando includo invece junit.jar scaricato da maven). Anche mvn test-compile crea correttamente la classe sotto test-classes. Sto gestendo questo su OSX 10.6.7 con Maven 3.0.2 e java 1.6.0_24.

Ecco la struttura della directory:

/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my_group</groupId>
    <artifactId>my_program</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>My Program</name>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ClassUnderTest.java:

public class ClassUnderTest {

    public int functionUnderTest(int n) {
        return n;
    }

}

ClassUnderTestTests.java:

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ClassUnderTestTests {

    private ClassUnderTest o;

    @Before
    public void setUp() {
        o = new ClassUnderTest();
    }

    @Test
    public void testFunctionUnderTest_testCase1() {
        Assert.assertEquals(1, o.functionUnderTest(1));
    }

    @Test
    public void testFunctionUnderTest_testCase2() {
        Assert.assertEquals(2, o.functionUnderTest(2));
    }
}

Fine del test mvn-X:

[DEBUG] Configuring mojo org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-surefire-plugin:2.7.1, parent: sun.misc.Launcher$AppClassLoader@5224ee]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test' with basic configurator -->
[DEBUG]   (s) basedir = /Users/aaron/Programs/my_program
[DEBUG]   (s) childDelegation = false
[DEBUG]   (s) classesDirectory = /Users/aaron/Programs/my_program/target/classes
[DEBUG]   (s) disableXmlReport = false
[DEBUG]   (s) enableAssertions = true
[DEBUG]   (s) forkMode = once
[DEBUG]   (s) junitArtifactName = junit:junit
[DEBUG]   (s) localRepository =        id: local
      url: file:///Users/aaron/.m2/repository/
   layout: none

[DEBUG]   (f) parallelMavenExecution = false
[DEBUG]   (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.7.1:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.7.1:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.7.1:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.0.5:compile, junit:junit=junit:junit:jar:3.8.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
[DEBUG]   (s) printSummary = true
[DEBUG]   (s) project = MavenProject: my_group:my_program:1.0-SNAPSHOT @ /Users/aaron/Programs/my_program/pom.xml
[DEBUG]   (s) projectArtifactMap = {junit:junit=junit:junit:jar:4.8.1:test}
[DEBUG]   (s) redirectTestOutputToFile = false
[DEBUG]   (s) remoteRepositories = [       id: central
      url: http://repo1.maven.org/maven2
   layout: default
snapshots: [enabled => false, update => daily]
 releases: [enabled => true, update => never]
]
[DEBUG]   (s) reportFormat = brief
[DEBUG]   (s) reportsDirectory = /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG]   (s) session = org.apache.maven.execution.MavenSession@dfbb43
[DEBUG]   (s) skip = false
[DEBUG]   (s) skipTests = false
[DEBUG]   (s) testClassesDirectory = /Users/aaron/Programs/my_program/target/test-classes
[DEBUG]   (s) testFailureIgnore = false
[DEBUG]   (s) testNGArtifactName = org.testng:testng
[DEBUG]   (s) testSourceDirectory = /Users/aaron/Programs/my_program/src/test/java
[DEBUG]   (s) trimStackTrace = true
[DEBUG]   (s) useFile = true
[DEBUG]   (s) useManifestOnlyJar = true
[DEBUG]   (s) workingDirectory = /Users/aaron/Programs/my_program
[DEBUG] -- end configuration --
[INFO] Surefire report directory: /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] Setting system property [user.dir]=[/Users/aaron/Programs/my_program]
[DEBUG] Setting system property [localRepository]=[/Users/aaron/.m2/repository]
[DEBUG] Setting system property [basedir]=[/Users/aaron/Programs/my_program]
[DEBUG] Using JVM: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-junit4:jar:2.7.1:test (selected for test)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.7.1:test (selected for test)
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.7.1/surefire-junit4-2.7.1.jar Scope: test
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: test
[DEBUG] Test Classpath :
[DEBUG]   /Users/aaron/Programs/my_program/target/test-classes
[DEBUG]   /Users/aaron/Programs/my_program/target/classes
[DEBUG]   /Users/aaron/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
Forking command line: /bin/sh -c cd /Users/aaron/Programs/my_program && /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/aaron/Programs/my_program/target/surefire/surefirebooter6118081963679415631.jar /Users/aaron/Programs/my_program/target/surefire/surefire4887918564882595612tmp /Users/aaron/Programs/my_program/target/surefire/surefire9012255138269731406tmp

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.089s
[INFO] Finished at: Mon May 30 12:03:09 EDT 2011
[INFO] Final Memory: 7M/62M
[INFO] ------------------------------------------------------------------------
Author: Michael Currie, 2011-05-30

17 answers

Per impostazione predefinita Maven utilizza le seguenti convenzioni di denominazione quando cerca test da eseguire:

  • Test*
  • *Test
  • *TestCase

La tua classe di test non segue queste convenzioni. È necessario rinominarlo o configurare il plugin Maven Surefire per utilizzare un altro modello per le classi di test.

 543
Author: axtavt, 2016-06-20 21:23:29

Ho anche scoperto che il codice di test unitario dovrebbe essere inserito nella cartella di test, non può essere riconosciuto come classe di test se lo si inserisce nella cartella principale. ad es.

Sbagliato

/my_program/src/main/java/NotTest.java

Destra

/my_program/src/test/java/MyTest.java
 63
Author: Robin Ma, 2015-11-19 14:15:45

Un'altra cosa che può causare Maven a non trovare i test se se la confezione del modulo non è dichiarata correttamente.

In un caso recente, qualcuno aveva <packaging>pom</packaging> e i miei test non sono mai stati eseguiti. L'ho cambiato in <packaging>jar</packaging> e ora funziona bene.

 49
Author: Jon, 2015-10-27 22:47:44

Inoltre, controlla se la directory delle classi di test (ad esempio src/test/java) corrisponde alla directory elencata nella proprietà <testSourceDirectory> nel tuo pom.xml sotto <build> proprietà. Ci ho messo un po ' a scoprirlo.

 23
Author: t3rmin41, 2014-11-02 08:46:23

Quando si utilizza JUnit 5, mi sono imbattuto nello stesso problema. Maven Surefire ha bisogno di un plugin per eseguire i test JUnit 5. Aggiungi questo al nostro pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.2.0-M1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0-M1</version>
        </dependency>
    </dependencies>
</plugin>

Fonte: https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven

 16
Author: teyzer, 2018-04-17 14:58:03

Verificare che (per JUnit-4.12 e Eclipse surefire plugin)

  1. Aggiungi la versione JUnit richiesta in POM.xml nelle dipendenze. Fare Maven - > Aggiorna progetto per vedere i vasi richiesti esportati nel progetto.
  2. Classe di test è sotto la cartella src / test / java e sottodirectory di questa cartella (o cartella di base può essere specificato in POM in config testSourceDirectory). Il nome della classe dovrebbe avere la parola tailng 'Test'.
  3. Il metodo di test nella classe di test dovrebbe avere annotazione @ Test
 9
Author: Mitra, 2015-01-06 08:22:50

Scoperto se si prefigge un test con 'Abstract', verrà ignorato anche per impostazione predefinita.

 8
Author: user1016765, 2015-10-28 10:52:40

Maven non eseguirà i test se il progetto ha <packaging>pom</packaging>

È necessario impostare la confezione su jar (o qualche altro tipo di artefatto java) per eseguire i test: <packaging>jar</packaging>

 7
Author: robjwilkins, 2017-02-28 09:45:55

Se hai un'applicazione Java / Groovy condivisa e tutto ciò che hai sono test unitari Groovy, Maven non troverà alcun test. Questo può essere risolto aggiungendo un test unitario in src / test / java.

 4
Author: bruce szalwinski, 2014-03-13 17:19:39

Ho anche avuto un problema simile, dopo aver esplorato che la dipendenza testng sta causando questo problema. Dopo aver rimosso la dipendenza testng da pom (dato che non ne ho più bisogno), ha iniziato a funzionare bene per me.

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>
 2
Author: rajesh madhavan, 2017-08-10 15:37:46
/my_program/src/test/java/ClassUnderTestTests.java

Dovrebbe essere

/my_program/src/test/java/ClassUnderTestTest.java

Il Maven trova quelle estremità Test o inizia con Test da eseguire automaticamente.

Tuttavia, è possibile utilizzare

mvn surefire:test -Dtest=ClassUnderTestTests.java 

Per eseguire i test.

 2
Author: Boris Z., 2018-02-23 08:49:14

Mi è capitato un altro motivo per non eseguire i casi di test: avevo una proprietà denominata "test" per scopi completamente diversi, ma interferiva con il plugin surefire. Quindi, si prega di controllare il vostro POMs per:

<properties>
  <test>.... </test>
  ...
</properties>

E rimuoverlo.

 1
Author: Rusi Popov, 2017-09-02 17:24:58

Un altro suggerimento (oltre alle risposte precedenti):

In Eclipse, vai alle Proprietà del tuo progetto > fai clic su Run/Debug Settings:

" Questa pagina consente di gestire le configurazioni di avvio con risorsa attualmente selezionata "

Lì puoi aggiungere (Nuovo...) o rimuovere (Eliminare) qualsiasi test JU (JUnit) che hai nel tuo progetto (nella cartella src/test/java o nel corso).

 1
Author: datv, 2018-05-03 10:39:34

junitArtifactName potrebbe anche essere il caso se il JUnit in uso non è lo standard (junit:junit) ma per esempio...

<dependency>
    <groupId>org.eclipse.orbit</groupId>
    <artifactId>org.junit</artifactId>
    <version>4.11.0</version>
    <type>bundle</type>
    <scope>test</scope>
</dependency>
 0
Author: Tuomas Kiviaho, 2017-04-14 09:27:22

Nel caso in cui qualcuno abbia cercato e non lo risolva, avevo una libreria per diversi test:

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${org.junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>

Quando ho installato junit tutto ha funzionato, spero e aiuto questo:

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
 0
Author: Hizmarck, 2018-03-20 05:22:48

Se il nome della classe di test non segue la convenzione di denominazione standard (come evidenziato da @axtavt sopra), è necessario aggiungere il nome del modello / classe nel pom.xml al fine di Maven scegliere il test -

...
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*_UT.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build> 
...
 0
Author: Saikat, 2018-04-17 18:26:15

Ecco il codice esatto che ho dovuto aggiungere al mio pom.xml:

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0-M1</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.2.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Ed ecco le mie dipendenze:

    <dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easytesting</groupId>
        <artifactId>fest-assert-core</artifactId>
        <version>2.0M10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>1.2.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0-M1</version>
    </dependency>
</dependencies>
 0
Author: anon58192932, 2018-08-15 04:02:04