Maven: Run Test On the JAR - maven

I've coded my own ApplicationListener for Spring-Boot that looks for files on the classpath. When running the test in Maven it looks for files in the filesystem as the JAR doesn't exist yet. That's fine and is a good test as it covers running the final application from within an IDE.
However, I also want to test my class when it's run inside a JAR (not necessarily as a repackaged Spring-Boot JAR) to see if it grabs the files inside the JAR. The tests-JAR contains all those files I want to read so theoretically it's all ready.
So how do I set this up with Maven? There's just one test I want to run after the JAR was built, all other tests are run as tests and integration-tests in the common lifecycle-phase (well, I have to filter out that one test for those phases somehow, too).

Related

Importing tests from another project using Gradle

I have a microservice that has a sub-folder full of integration tests written in Groovy. These tests basically make a bunch of REST calls. Gradle is configured to produce a JAR for these tests like so:
//Explicitly add the integration tests to the JAR
jar {
from sourceSets.test.output
}
Unzipping the JAR shows the Groovy tests that were in the project. That seems good so far.
Now, there is a larger project that I want to use (in a different git repo) that will import these tests and run them. The plan is to import several other test suites in a similar fashion and build a sort of uber-test project. This project can import the test JAR as a dependency as testCompile. I can see the JAR in the SourceSets using IDEA's gradle plugin. However, I can't get the tests to run using gradle test.
How can I modify the uber-test project to execute these imported test?

Invoking maven from inside an uber jar using pom.xml located inside jar

I am running a set of tests using junit and I have created a maven project for it. I use the surefire plugin to run the tests. I wanted to package this project as a self executable jar for the sake of convenience.So I placed all of test classes in the src/main/java directory as recommended for using the maven shade plugin for generating an uber jar. I created a class with a main method and use maven invoker to execute my tests.
The tests get executed perfectly when I use my ide to run the main method. However, after I package it as a jar , the invoker is unable to locate the pom.xml. If I place a copy of the pom.xml in the same directory as the jar, maven is invoked but it is unable to execute the tests as surefire is unable to locate the tests.
I would love to know if there is a solution for this or if there is a better approach towards trying to achieve my goal.
#khmarbaise and everyone else trying to help me , let me describe my scenario a little more in detail.
Lets take any api , for example , lastfm api, I would be writing classes for each api end point and every method in each class would be a test. I use junit to execute the tests. I use a junit wrapper called serenity bdd that helps me structure my tests and generate aggregate reports. The maven goal serenity:aggregate would generate an aggregated report of all tests. I am using the maven invoker to run tests using surefire plugin and then aggregate the tests using serenity. In my eclipse project I simply create a maven run configuration and provide the maven goals. Or else I would use the terminal to run maven from my project base directory.
Apart from my class file, I have several resources, like csv files which are inputs for parametric tests a log folder where all log files that are generated get saved and other sample files for testing file upload apis. I felt that it would be simple to package all of this as a jar and let my developers add this jar as a dependency and run a simple script which runs the jar each time they create and deploy a build. Getting the pom file for invoking maven was trivial in case of eclipse or the command line but not so straight forward when its inside of a package.

gradle and dependency on spring-boot subproject

I've created a project with several gradle subprojects, including: "app" and "tests".
Tests have "app" in their dependencies. Tests use classes from "app"
When I run:
./gradlew clean test build
Everything works, tests run and pass.
But when I run:
./gradlew clean build
then the tests compilation fails with an error saying that a class is missing - in this case it's a spring-boot configuration class. I run this with --debug and it turns out that in the failing case app:bootRepackage task is executed before tests:test, the jar generated by app compilation is altered and that's why the classes cannot be found.
How can I make "./gradlew clean build" work properly?
Using: spring-boot 1.5, gradle 4.0 (and 4.1 too), io.spring.dependency-management plugin 1.0.0.RELEASE
Ideally, you shouldn't use a Spring Boot application (something that's been repackaged) as a dependency. From the documentation:
Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
If the proposed solution isn't possible in your situation, the documentation goes on to describe an alternative:
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.
To configure a classifier of exec … when using Gradle, the following configuration can be used:
bootRepackage {
classifier = 'exec'
}

In a Maven project, how can you configure the JUnit tests to use the project's own build artifact as a Java agent?

I've created a Maven project. When the project is built, it produces a JAR file that can be used as a Java agent. I have written tests and included them in the project. The tests make sure the Java agent works. In order for the tests to use the Java agent, the JAR file needs to have already been built and an argument needs to be passed to the VM that includes the location of the JAR file. Is this possible with Maven? If so, how?

TestNG execute compiled tests in jar file

I have a testNG test class which runs fine, but when i package it into a jar file with test files included, i am unable to run the tests from it. The jar file contains the test.class file in it. When i invoke the test from maven it returns "no tests to run".
Any suggestion on how to run a test class within a jar using testNG?
Can you open the jar file with winrar and see if everything is packed there?
Also, is testng your application entry point? What happens when you write:
java -jar yourjar.jar
in console? You should see testng output.
Also, are you sure your parameter xml file is pointing to the right class and that it works?
Are your classes in the jar file? the classes with your tests? Please check that with winrar to see the jar content.
I think that with this checklist your tests should run.

Resources