Running test cases from jar maven - java

I have a .jar file, where I have packaged my maven managed java project which has few Junit test cases as well. Now I want to run test cases from my .jar file. For example, from the source code mvn test will run the test cases, but it is not working for .jar file.
How do I run Junit test cases from my .jar file?

You need to configure the maven-jar-plugin, and include the test classes in the jar
http://maven.apache.org/plugins/maven-jar-plugin/usage.html
have a look also here
http://maven.apache.org/plugins/maven-jar-plugin/test-jar-mojo.html

Related

Is it possible to run Junit test for a JAR file

I am completely new to JUnit, I have a jar file, which is getting build using ant, now my Junit is a maven project, so I want to run the JUnit test cases on the already compiled jar file. Is it possible?

Configuring JUnit Directory Structure in Maven

I have a Java Spring application with unit tests in the directory
src/test Specifically src/test/com/client/rest
I'd like to add a child directory here, something like
src/test/com/client/rest/controllers
Which contains the unit tests for all controllers of the application. I created a file in this directory with a "#Test" end-point, but did not do anything to POM.xml. When I run mvn clean package I get the following error
The goal you specified requires a project to execute but there is no POM in this directory [path]. Please verify you invoked Maven from the correct directory.
I tried adding this new directory as a testResource element under build in POM.xml, but that did not work. Prior to adding this new file, the tests ran fine with <testSourceDirectory>src/test</testSourceDirectory> under the build element.
How do I properly integrate this new JUnit directory into Maven?
EDIT: I moved my new file to the standard parent directory where the other JUnit test files are (src/test/com/client/rest) and it seemed to register fine. What is the best practice for storing JUnit class files, and is it worthwhile to try to create new child directories for organizational purposes?
I think the error comes from just running the mvn command from somewhere other than your project root.
How do I properly integrate this new JUnit directory into Maven?
Follow the Maven Standard Directory Layout - https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
Your tests should be under src/test/java/com/client/rest/controllers . They should then be discoverable and just work without any additional Maven configuration.
What is the best practice for storing JUnit class files, and is it worthwhile to try to create new child directories for organizational purposes?
It is worth organizing your tests. I think the piece you're missing is that you are writing tests in Java so you need to organize your tests with both directories and packages. So a test located under src/test/java/com/client/rest/controllers would have a package of com.client.rest.controllers .

Force JUnit gradle task to use the jar build in the project

I have a gradle project that generates a jar ( myFoo.jar ) in the build/libs folder.
I have a few JUnit test that tests the classes from the myFoo.jar. The JUnits runs fine, except for the fact that when running the tests, the tests runs with bin/ in the classpath and the class loader is not really loading the build/libs/myFoo.jar. The tests instead run with bin/ in the classpath. So the JUnits passes except for a few.
One of my tests verifies some of the files under the resource folder in the jar, and these tests are failing with the 'test' task is run.
Is there any way for the gradle test task remove the bin/ from the classpath and instead include build/libs/myFoo.jar ?
Id used the following code to fix the issue. This statement modifies the classpath for the tests and precedes the classpath with the jar gradle creates.
test {
classpath = "$buildDir/libs/"+jar.archiveName,
"$buildDir/classes/test",
configurations.runtime
}
Gradle Fourm https://discuss.gradle.org/t/force-junit-gradle-task-to-use-the-jar-build-in-the-project/18543 for details.

How to specify Specification-Title without creating jar

When running the test phase my tests fail to the JAI requirement for the code to be run from inside a jar file that contains Specification-Title in the manifest.
Unfortunately mvn test and intellij debug do not build a a jar file (obviously) and so no manifest exists.
Is there an alternative way of setting these values?

how to use cobertura code coverage tool with jar file

I'm using cobertura to find out code coverage of my test suite, which tests a java based solution bundled through a jar file, cobertura cmd line reference
I have done the instrumentation part and obtained the instrument class files.
For the second step, I'm not sure whether I need to re-bundle the instrumented class files into a jar file since my test suite uses this jar file or can I simply set my CLASSPATH variable to include the instrumented class file while continue to use original jar file (which contains the uninstrumented class files).
Thanks.
You have to include your instrumented files in the classpath BEFORE any other containers/Folders that may include the same compiled files.
In you case before the jar file with the instrumented files.
You don't need to rebundle.
Another possibility would be to instrument the jar file directly.

Categories

Resources