I am using maven to make a simple application and writing unit tests for it. I am new to both using maven as well as writing unit tests with Junit.
My directory structure is as follows :-
The source code classes used in my project are in the directory : -
project-name/src/main/java/com/somename/app/packagename
Main file (which contains static main method) is in :-
project-name/src/main/java/com/somename/app/App.java
The default unit test provided by Maven is in :-
project-name/src/test/java/com/somename/app/AppTest.java
Now, I have added a unit test in the above directory (in which AppTest.java) is there and this unit test needs to access the classes defined in the "packagename" directory as described above.
If it were a simple commandline , I would have added the classpath to the directory when compiling and running the application.
How should I do the same in Maven ? Also, is there any alternative to adding the classpath directly in maven ?
When using the Maven Standard Directory Layout (which I always recommend as Maven is all about conventions), the following directories are always part of the classpath when running the tests:
src/main/java
src/main/resources
src/test/java
src/test/resources
So there is no need to manually adjust anything for running the tests. The mvn test command simply does what you want.
Maven Directory structure is always added in the class path. if you are using eclipse just got to Run as->maven build.(make sure you have maven plugin installed) otherwise you can directly use the mvn clean , mvn install command for building the project in maven.
Related
i have a multi-modules maven, which is configured like that :
sub-module maven : modul-main : contains the Main class and config folder with an application.properties file
sub-module maven : modul-common : an another sub-module
if i update my code in modul-main, so the command
mvn spring-boot:run
see my changes at run
but IF i update my code in modul-common, so the command
mvn spring-boot:run
does not see my changes!
how explain that ? (what the command does in background ?) want to understand !
is there an option with this line command to solve this ?
That's how Maven works and this has nothing to do with Spring Boot. When you are in module-main and you run a command, it only impacts that module and Maven is not aware this module is part of a larger project that should be updated if necessary.
When you are in a particular module, dependencies are picked up via the local maven repository (not the individual target/classes locations for each module).
This is a significant difference compared to how Gradle works (running a command in a module is going to update sibling module automatically if needed).
I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time.
Is there a way to first run the JUnit and only if there were no test error run the project itself ? I want them to run together with 1 click (NOT run them seperately/manually).
Also, i would like the above to work even when the project is packed as a .jar file.
How can it be done ?
Thanks !
In Intellij:
Run -> Edit Configurations
Create a JUnit configuration for your tests
Create a Run configuration for your project.
And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1.
It doesn't matter how your project is packed (jar, ...)
Normally this is done by using a build management tool like: maven, gradle, ant. In this way the build tool will run tests for you and stop if they fail.
With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code.
See example project: https://github.com/HaveACupOfJava/maven-jar-demo
I have inherited a codebase whereby we have a maven project of component tests that use junit as the framework that runs the tests.
Everything runs OK, but the problem is that these tests must be compiled into a jar-with-dependencies so that they can be run on a standalone computer with very limited outwards connectivity (e.g does not have access to maven central).
I have managed to create a runnable jar with dependencies which I can run using junit.jar and the command line like so:
java -cp jar-with-dependencies.jar:junit.jar junit.jar org.junit.runner.JUnitCore com.testsuite.AllTests
This works but the problem is that I also need to output the junit results into XML, like maven's surefire plugin does.
My question is, can I run this jar-with-dependencies using maven such that it creates the junit xml reports?
I can successfully run the tests using exec-maven-plugin which essentially runs the previously stated command
I have a jar installed in my local ~/.m2 repo and I would like to execute a single test using the -Dtest option via a python script. I tried using this command on the command line mvn surefire:test -DdependenciesToScan=groupId:artifactId -Dtest=NameOfTest, however it doesn't seem like maven is finding the NameOfTest in the groupId:artifactId dependency and returning back with no tests executed? Any way to execute a single test in an already installed maven artifact?
Typically Java classes in src/test/java (or your corresponding test sources directory) will not end up in a built artifact by maven by default. If you inspect the contents of the JAR, you will likely notice no compiled test classes, which is why maven can't find them.
If you really want your test source compiled into the JAR, there are plugins to help you. Particularly, the standard Maven JAR Plugin.
However, I would suggest you consider carefully why you need test classes in a built artifact. The standard use of test suites is to test the main source code being built. There are some situations that have been argued where having tests in the final artifact are valid, but they are rare and usually can be worked around in other ways ( Related discussion ).
When deploying an Arquillian test with Intellij using pom dependencies like:
File[] libs = Maven.resolver().offline()
.loadPomFromFile("pom.xml")
.importRuntimeDependencies()
.resolve().withTransitivity().asFile();
The test run just fine in other IDEs, but with IntelliJ, there is an error that does not let execute the test properly.
Go to Edit Configurations (under Run menu), select Defaults branch and
then JUnit. You can see that Working directory is set to project’s
directory. That’s why pom.xml could not be found in this location.
Fortunately it can be changed either to MAVEN_REPOSITORY or to
MODULE_DIR and the latter is exactly what you want. Changing that and
saving it in defaults makes the issue above disappear.
Now you can run your tests from IntelliJ even if you have maven
dependencies to be included in your #Deployment archive.
As seen here: http://michalostruszka.pl/blog/2012/10/24/arquillian-tests-with-maven-deps-intellij/