Importing tests from another project using Gradle - 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?

Related

Maven: Run Test On the JAR

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).

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 multi-module project does not run tests in one of the modules

We have a Gradle 5.x multi-module project consisting of four modules written in Java and a plugin written in groovy. When the modules were in separate git repos, we had coverage for the plugin because the tests ran. With everything in one repo, it now no longer runs the groovy tests, yet it runs all of the Java tests. We've tried setting the classes directories explicitly in test {
jacoco {
includes = [dirs here...]
but it has no effect. There doesn't seem to be anything online about this problem. Has anyone seen it? If so, what's the fix?

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'
}

Gradle build not invoking JUnit

My gradle script for building an EAR is not running any JUnit tests I have. The command I used for invoking the script is
gradle build
I also tried
gradle test
and that also did not work.
My build structure is as follows
settings.gradle
gradle.properties
EARProject
build.gradle
META-INF
application.xml
lib
JARProject
build.gradle
src/main/java
....
src/test/java
....
WebProject
build.gradle
src/test/java
src/main/java
WebContent
.....
I am invoking the gradle command from EARPRoject directory.
What changes I need to do in order to run the test cases. Please note that if I run the gradle build command from individual projects it is working as expected.
Regards
-Albin
Apparently, EARProject doesn't have any tests. Depending on what you want, run gradle build from the top directory, or from a subproject/subdirectory containing tests. Alternatively, you can run gradle buildNeeded from EARProject, which will perform a full build of all projects that EARProject depends upon (which presumably includes JARProject and WebProject).

Resources