0

There are a bunch of questions related to the issue that mvn test doesn't execute test classes where the answer is always, that maven-surefire looks for certain prefixes or postfixes of filenames. I already considered this and it didn't help me as my filenames where already valid.

When I execute mvn test, the log contains the following section:

[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ compiler --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 5 source files to /project_path/target/test-classes

... so mvn test does actually find my test cases, and indeed, I can find all the compiled test classes in the directory /project_path/target/test-classes/
But still, only one of these 5 test files is actually executed and its tests are run whereas the other 4 are ignored by mvn test.

Any ideas why that could be the case? I am not really sure which additional information could be helpful to answer this question. Just ping me if there is something that I need to add to the question.

Lavair
  • 888
  • 10
  • 21
  • comment out the inside of that first test case, and try it again, see if it goes to the 2nd test... if so, it means that it is stopping on first failure. in gradle there is an option to continue on first/any failure, might be similar in mvn – mawalker Jan 30 '21 at 15:26
  • Have you tried using `mvn clean test` to ensure that the full project is built from scratch? – Bill Naylor Jan 30 '21 at 15:30
  • @BillNaylor I've tried mvn clean test, yes. Also manually deleting the target folder, reloading the maven project etc.. – Lavair Jan 30 '21 at 15:34
  • @mawalker when i execute the test cases in the IDE, they are all green. So I also don't think that this is an issue here. But just to be sure, I just moved out that test file completely from my project and rerun mvn clean and mvn test and it didn't help. Now, 0 test files were run. – Lavair Jan 30 '21 at 15:40

1 Answers1

0

I just found a way to fix it..

Honestly, I don't understand why this makes the difference, but I have used @Test annotations on my test cases from org.junit.jupiter.api.Test in the file that was executed correctly by mvn test and used @Test annotations from org.junit.Test in the other files (which were not executed).

I've changed all files to import the @Test annotation from the jupiter package and then all tests were recognized.

Lavair
  • 888
  • 10
  • 21
  • This means you have defined JUnit Jupiter as a dependency or using it otherwise (may be via spring, spring boot).. JUnit Jupiter is something different that JUnit 4... – khmarbaise Jan 30 '21 at 16:09
  • So Junit Jupiter is Junit 5, whereas org.junit.Test is Junit 4. If you really must persist with the two different versions, I found an Answer which allows that: https://stackoverflow.com/questions/47158583/executing-junit-4-and-junit-5-tests-in-a-same-build#:~:text=1%20Answer&text=JUnit%205%20provides%20a%20way%20out%20of%20the%20box.&text=Each%20one%20is%20a%20distinct,tests%20in%20a%20same%20project. – Bill Naylor Jan 30 '21 at 16:27