2

I compiled the tests and I am trying to run them doing the following:

> java -jar lib/junit-platform-console-standalone-1.0.0-M4.jar --class-path ./bin --select-class com.mycompany.DbTest

This throws a massive exception that starts like this:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/commons/JUnitException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
...

But basically says it can't find the JUnitException class. If instead I let the console launcher look for the tests, it finds no tests:

> java -jar lib/junit-platform-console-standalone-1.0.0-M4.jar -p bin/
╷
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔

Test run finished after 20 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          ]

I am at a loss to know how can I run JUnit5 tests from the command line, without IDEs.

Update:

Using Maven to run the tests (with the Surefire plugin) gives me the same errors. I even removed all the tests but one that merely imports junit5 dependencies, and I still get the error.

Sergi Mansilla
  • 12,495
  • 10
  • 39
  • 48

2 Answers2

1

Your inital command line contains a duplication. A single "java -jar ..." should allow the Java runtime to find the bundled JUnitException class.

Use options --class-path and --scan-class-path together to get the automatic scan running.

Something like: java -ea -jar lib/junit-platform-console-standalone-1.0.0-M4.jar --class-path bin --scan-class-path

If you want to use a selector, like --select-class, you still have to provide the custom classpath with --class-path bin. Otherwise, only the standalone jar is on the classpath, which does not contain your tests.

Or there are JARs in the Mac Extension Libraries folder that interfer with the test execution. Please remove them.

Sormuras
  • 8,491
  • 1
  • 38
  • 64
  • The command duplication was not the problem. The command you suggest gives me the exact same problem, JUnitException is not found. – Sergi Mansilla Jun 26 '17 at 20:53
0

As said in the comments, having JUnit 4 on the classpath was the problem for me as well. Make sure to remove all traces of it, either from macOS extensions folder or from your gradle dependencies. I had to use

configurations.compile {
    exclude group: 'junit'
}

to force remove it from everywhere, including transitive dependencies.

Roman Plášil
  • 1,941
  • 2
  • 17
  • 27