问题
I've been trying to run JUnit 5 tests with Maven Surefire. However, it doesn't seem like Surefire is running any tests at all, even though I do have some, and in the default directory, too.
This is the console output I'm getting: https://prnt.sc/ugo1xt
Here are the relevant parts of the pom.xml:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0-M1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
The surefire version is 3.0.0-M4.
I've tried pretty much any fix I could find on Google, although most of them seemed to be outdated. Any help would be greatly appreciated.
Cheers!
EDIT: Here's an example of my tests:
package bankprojekt;
import bankprojekt.verarbeitung.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class GirokontoTest {
Girokonto gk;
@BeforeEach
void setup(){
gk = new Girokonto();
gk.einzahlen(500);
}
@AfterEach
void teardown(){
gk = null;
}
@Test
void abhebenMitWaehrungswechsel(){
try{
gk.abheben(195.583, Waehrung.BGN);
}
catch (Exception e) {
System.out.println(e);
}
assertEquals(400, gk.getKontostand());
}
@Test
void einzahlenMitWaehrungswechsel(){
gk.einzahlen(195.583, Waehrung.BGN);
assertEquals(600, gk.getKontostand());
}
}
回答1:
There are two things. First you should upgrade jacoco dependency to 0.8.5 otherwise it fails based on the JDK14 requirement. (Created an pull request to your repository). If those things are configured appropriately the result is this:
I would also recommend to upgrade maven-compiler-plugin to most recent version and also all other plugins.
As you can see you have WARNING's in your build which should be fixed and failing tests.
This build has been run on plain command line also with JUnit-Jupiter 5.7.0 which works perfectly.
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/classes
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java uses or overrides a deprecated API.
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: Recompile with -Xlint:deprecation for details.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ prog3-sose2020 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/khmarbaise/ws-git-so/prog3-sose2020/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ prog3-sose2020 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running bankprojekt.GirokontoTest
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.087 s <<< FAILURE! - in bankprojekt.GirokontoTest
[ERROR] bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel Time elapsed: 0.052 s <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <448.87081188037814> but was: <400.0>
at bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel(GirokontoTest.java:31)
[INFO] Running bankprojekt.WaehrungTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in bankprojekt.WaehrungTest
Kunde Mustermann, Max zerst�rt
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] GirokontoTest.abhebenMitWaehrungswechsel:31 expected: <448.87081188037814> but was: <400.0>
[INFO]
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.792 s
[INFO] Finished at: 2020-09-14T17:50:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project prog3-sose2020: There are test failures.
[ERROR]
[ERROR] Please refer to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
来源:https://stackoverflow.com/questions/63875520/maven-surefire-not-running-junit-5-tests