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

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

Related

Maven Test with Evosuite-generated Test Cases

I am using Evosuite to generate test cases for my app via Maven and I've followed all of the steps that are outlined in the Evosuite documentation.
I see that all of the test classes have been generated and the export copy the classes to the test folder of my project, so I should be able to run mvn test to run the tests, but when I do, I get a series of errors that it cannot find a bunch of classes (it looks like it can't find any of the Evosuite classes from the runtime even though I have the evosuite runtime defined as a dependency in my POM.)
I would love to use Evosuite for all of our apps but if I cannot get the mvn test to run without errors then the product is useless. Can anybody help with this? I have gone over the documentation several times and checked everything and it all appears to be configured correctly. Thank you.
If you have already run mvn evosuite:export to move them to the ./src/test folder then you may need to compile them using javac so they can be executed by maven. Try using this article to compile them all within one command: How to compile multiple Java files when there are Java files in other packages?

How to run tests in a folder in maven

I am using spring-boot. I have my tests segregated in the following manner
tests
|java
|endtoend
|org.mycompany.tests
|integration
|org.mycompany.tests
|unit
|org.mycompany.tests
When I run a mvn test or package everything inside tests folder are running. I want to run the test within individual folders. Intellij is able to run unit, integaration and endtoend separately.
I saw posts for running tests within a package. All my different test folders have the same package structure. So filtering by test packages will not help me.
If intellij is able to run the folders individually, I think I am just missing the command to run it from the cmd line. Please advise.

Maven test scope dependency change does not recompile module's tests

We have a maven multi-module project in which the following weirdness occurred:
module-x has a TEST (src/test/java/...) which depends on code provided by module-y, whose code is not otherwise used in module-x (i.e. nothing in src/main/java/... depends on module-y)
module-x therefore defines a dependency on module-y, but with <scope>test</scope> and uses that code in one of its tests
someone changed a constructor in module-y and committed the code that broke the module-x test (as it was using the old constructor parameters)
TeamCity ran java org.codehaus.plexus.classworlds.launcher.Launcher -f app-parent/pom.xml -B integration-test when it saw the commit
the build succeeded because maven did NOT recompile/rerun module-x TEST code, it just recompiled module-y and everything that depends on it with default compile scope
I'm sure people will point out, why is that test even in module-x, etc, but this weird setup aside for a minute. What I want to understand is this:
If some project (module-x) has a test dependency (module-y) that changes and is recompiled, should not maven then notice this and do a fresh "test-compile" for the project (module-x) that "test-depends" on what changed (module-y)? Is what I saw here normal behavior?
EDIT: from comment replies it seems like the fact that TeamCity is being used may be a factor; I clarified the command used in the sequence list above.
EDIT 2: I should note that the parameter "-f app-parent/pom.xml" is actually the main module that basically depends on "the world" and is where one would run from command-line "mvn test" or "mvn clean package" to rebuild everything.

Running a single test with invoker plugin

Following is the directory structure for my integration tests
/src/it/first-test
-->my-test
-->build.log
-->inoker.properties
-->pom.xml
-->verify.groovy
When I try to run a single integration tests as described https://maven.apache.org/plugins/maven-invoker-plugin/usage.html. It gives a message that ' No projects were selected for execution' Here is the command I used to invoke the project
/src/main> mvn invoker:run -Dinvoker.test=first-test/my-test*
How should I make sure the test is run?
It looks like you misunderstood the docs how to structure your integration tests. The first integration test should be located /src/it/first-test the second integration test should be located /src/it/second-test which means your folder my-test should be removed...Furthermore you should start the integration test from your project root and not by mvn invoker:run you should use mvn verify -Dinvoker.test=first-test instead...
It looks like you are executing it from src/main. Try it again from the root of the project (where the pom.xml is located).

Having issues running java project through command line

I am completely new to using maven. I have created a maven project and exported it to eclipse. Maven automatically created the src/test/java and src/main/java.
I created a java script and successfully ran it in eclipse. But when I try running it through command line, I got an error that says:
cannot find or load main class.
When I checked my project directory, there were two classfile paths: classes and test-classes. The script I was running is the one in 'test-classes' but it is not the main class. But the path to the main class executes successfully but that is not where my script is located.
The command I was using for the main classfile is:
java -cp target/Test-1.0-SNAPSHOT.jar com.mycompany.App.
The command for the test classfile is :
java -cp target/Test-1.0-SNAPSHOT.jar com.mycompany.AppTest.
This second command gives me the error I mentioned.
Please how do I get around this issue?
By convention, everything in src/test/java is supposed to be for testing only. That is, you should place only your test classes in this directory. Maven will run them before building the final JAR, but it will NOT include them in it.
If you absolutely need a JAR with your test classes, have a look at How to create a jar containing test classes.

Resources