Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a test class from Maven's main folder?

Tags:

maven

testing

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?

like image 943
Erel Segal-Halevi Avatar asked Jan 29 '26 01:01

Erel Segal-Halevi


1 Answers

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

like image 176
kostja Avatar answered Jan 31 '26 16:01

kostja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!