I created a Maven project with the standard folder strucure - i.e. src/main/java, src/test/java, etc.
I wrote a class ClassA and a test class TestA.
From the main program of ClassA, I refer to a static member of TestA.
The code compiles, but when I run it, I get: NoClassDefFoundError: TestA.
How can I access TestA from within ClassA?
Instead of solving your issue directly, I would advise to rethink your test design. Maven is perfectly capable of running tests on its own, just enter
mvn test
on the command line. If you want to run a single test class, enter
mvn test -Dtest=MyTest
for a single test method, use
mvn test -Dtest=MyTest#shouldRunPerfectly
It also supports wildcards, so to run some common tests, you could type
mvn test -Dtest=Integration*#shouldBeFaster*.
Most IDEs allow to run tests directly by a shortcut. If I recall correctly, It's Shift+Alt+X then T for Eclipse and Shift+Ctrl+F10 for IntelliJ. IntelliJ also uses the Ctrl+Shift+T shortcut to navigate to the test of the class you are working with.
Maven directory structure emphasizes the separation of the tests from the application and makes it much harder to do what you are planning to.
tl;dr - do it the maven way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With