Is it possible to run Junit test for a JAR file - java

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?

Related

Disable test resource file compilation in Eclipse for Gradle project

My JUnit tests need Java source code files as input. I've created those files under src/test/resources. I read a test resource file from src/test/resources (e.g., src/test/resources/files/SimpleTestClass.java via
final String expectedClass = new String(ClassLoader.getSystemClassLoader()
.getResourceAsStream("files/SimpleTestClass.java")
.readAllBytes());
The behavior that I expect is that the test resource Java files will not be compiled. This is exactly what Gradle does, therefore, running the tests with gradle test works fine.
Unfortunately, running the JUnit tests from within Eclipse results in a compilation of those test resource Java files. The bin folder now includes .class files instead of the .java files.
I could exclude the test resource Java files from the compilation. However, this will also disable Eclipse's Java editor functionalities for those files, e.g., error highlighting or refactoring.
For the test resource Java source code files: How can I keep the Java editor functionalitites in Eclipse, but exclude the compilation of those files so that they still are copied to the bin folder?

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?

Running test cases from jar maven

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

Categories

Resources