Jim Weaver
Jim Weaver

Reputation: 1033

JUnit5 Console Launcher not finding tests

I've seen a number of posts on this, but none of the solutions seem to work for me.

The problem is that the JUnit5 console launcher cannot seem to find my tests, regardless of syntax that I try. I've tried scanning for them with --scan-classpath and specifying the class directly with -c, including the package specifier, which generates a NoClassDefFound.

With --scan-classpath the console launcher runs successfully, but doesn't include any unit test executions - doesn't find the test classes for some reason.

The tree looks like this:

junit-5-jars  (the console standalone jar is in here) 
src   
   mypackage
     MyClass.java
     MyClass.class 
test   
   mypackage
     MyClassShould.java
     MyClassShould.class

From the root of that, the first and most basic command I tried:

java -jar junit5-jars/junit-platform-console-standalone-1.7.0-all.jar --class-path=test --class-path=src --scan-classpath

All variations I've tried give me:

Test run finished after 27 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         2 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]

This is on a Mac from bash. In a project with all class files at the root of where I'm running the command, I can run tests with the console launcher successfully. But if class files are in subdirectories like this, no go. Anyone have ideas? (Note - see answer below, subdirectories were not the issue - the "Should" in the test class name was).

Upvotes: 3

Views: 2219

Answers (1)

Jim Weaver
Jim Weaver

Reputation: 1033

I think I've discovered the source of this problem - it was the "Should" in the test class name, which JUnit5 ConsoleLauncher does not include to be scanned by default.

This works:

java -jar junit5-jars/junit-platform-console-standalone-1.7.0-all.jar --class-path=test:src --include-classname=.* --scan-classpath

The include classname option takes a regex, but in my case I just let it scan all the files on the classpath for tests, and it finds them as it should.

Would be helpful if the --scan-classpath description in their docs referenced the include-classname option and/or their default scanning inclusions. Easy to miss that argument in that wall of options.

Upvotes: 2

Related Questions