Applying 1 junit-platform.properties file to entire project (multiple modules) - java

How can I apply a single junit-platform.properties to all modules in my project.
My project has 3 separate modules, each with its own test + resources.
Currently I'm duplicating and placing them in each resources directory. How can I reduce it to just 1 centralized junit-platform.properties and where should I place it?

The junit5 guide 4.5. Configuration Parameters describes
The JUnit Platform configuration file: a file named
junit-platform.properties in the root of the class path that follows
the syntax rules for a Java Properties file.
Therefore you could create a common project with your junit-platform.properties and add this project into the classpath for each of your projects.
for Eclipse workspace
Create baseproject with junit-platform.properties file.
junit-platform.properties with custom conmfiguration e.g. junit.jupiter.displayname.generator.default
junit.jupiter.displayname.generator.default = org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
Other project project2 without any own junit-platform.properties
add baseproject to java build path of project2
Launching Junit test added baseproject into the classpath
Result of Junit test shows test display name with replaced underscores
for IntelliJ workspace with gradle
Using IntelliJ workspace , e.g. junitconfig.
Add module baseproject for junit-platform.properties (as resource)
Add module project2 for your Java files and Junit test files.
Add module baseproject as dependency for module project2 in build.gradle
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation project(':baseproject')
}
rebuild module project2 and run junit test

Related

Gradle inject root project class path to dependencies

I am building a library, which has separate entry points (like server, gui), with different dependencies. Each entry point is in a separate sub project with its own dependencies.
In the root project, where I start the build from, I want to select the entry point, and only build with that dependencies. That is working.
But I want to instantiate a class (eg MainClass) of the root project from the library entry point and I cant add the root projects class path to the dependency. (Diagram)
Root projects build file looks like this now:
dependencies{
implementation project(':server')
}
It seems to me that it would be easier to understand and clearer if the entry point projects depended on the core API instead of vice versa.
You could have project structure like:
settings.gradle
core/
build.gradle
src/
server/
build.gradle
src/
gui
build.gradle
src/
server and gui project build.gradle files should contain:
dependencies {
implementation project(':core')
}
The project that uses the library could depend on Server and/or GUI projects and instantiate the necessary class (ServerEntry or GuiEntry) directly.
If you want to be able to switch between different entry point implementations without changing the code in the project that uses the entry point instance I'd suggest using a dependency injection framework (Spring, Guice, Dagger). Dependency injection would help to separate configuration (binding interfaces to classes) from the actual application.
I solved my problem with Composite build. I added includeBuild '../path-to-lib' in the settings.gradle, Than I created a subproject Project to the library with the proper package and class name. On run it throws an error, that the developer should create this class.
Its also important to add all the subprojects to the same group:
allprojects{
group = 'library-group'
}
In the host project, I can depend on the library:
dependencies{
implementation module('library-group:suproject')
}
Now Gradle automatically overwrites the Classes on the same route as the documentation suggest, and I can finally inject my host project into the lib, and compile it as a whole.

Gradle spring boot with assembly module

I'm new to Gradle and I'm facing a problem ( using Gradle 2.14)
My project has 3 subprojects: assembly, sprinBootProject, E2E
The assembly module is responsible of zipping the jar of the spring boot application along with properties files.
The E2E module is an 'end to end' tests module for testing the spring boot application.
I'm using the distribution plugin in assembly/build.gradle like this:
apply plugin: 'distribution'
distributions {
main {
baseName "project-assembly"
contents{
from project(':springBootProject').fileTree(dir: 'build/libs', include: ['*.jar'])
from project(':springBootProject').fileTree(dir: 'build/resources/main', includes: ['*.properties', 'conf/*.properties'])
}
}
}
but I need to make sure that springBootProject will be evaluated before the assembly subproject, so I used dependsOn like that:
distZip.dependsOn(':springBootProject:build')
when I added this line I see that the E2E module is failing in compileTestJava task, so this line is effecting how the spring boot application is generated (I think)
E2E/build.gradle:
dependencies {
testCompile project(":springBootProject")
}
so my question is am I doing it right? why when I added the dependsOn line the E2E module is effected, and how can I force Gradle to evaluate the spring boot application before the assembly module?
I figure it out.
Gradle will evaluate modules by alphabetic order
so the evaluation will be: assembly -> E2E -> springBootProject
when adding this line to assembly/build.gradle:
distZip.dependsOn(':springBootProject:build')
the order of the evaluation changed to be: assembly -> springBootProject -> E2E
now in 'springBootProject' module I am using the spring boot plugin in order to generate an executable jar, this executable jar is generated by the task :springBootProject:bootRepackage which uses the jar generated by the :springBootProject:jar task and convert it to executable.
now because the 'E2E' module depends on 'springBootProject' it will use it's jar, but now this jar is executable, and because of that I had a compilation error in 'E2E'.
so I have to make sure that the 'E2E' module gets the original jar and not the executable one.
so I added this line in springBootProject/build.gradle:
bootRepackage.mustRunAfter(':E2E:test')
which seem to solve the problem

Java Gradle find out dependency's jar file name

I have a java gradle project. I have a dependency.
dependencies {
compile project(":mymodule")
compile 'org.springframework:spring-context:4.1.2.RELEASE'
}
Where can I find and use exact jar file name of both my custom module and spring dependency jar to process it further in composing application's libs, folders, etc?
Stick this in a new task:
project.configurations.compile.each{ println it}
Or, for just one specific dependency:
println project.configurations.compile.find {it.name.startsWith("something") }
However, if you're looking to create distributable packages including dependencies, you really should look into the gradle application plugin.

Cucumber-JVM Runner class in different folder

I am new to Cucumber and learning and trying to automate application. I created a new Test Project. I want to keep the feature files and Cucumber runner class in a different folder and all the glue code in another folder.
How do I specify the feature file path under #CucumberOptions?
The project folder structure is as follows:
Project-2
|-src
|-test
|-java
|-mypackage
|-CucucmberRunner.java
|-resources
|-MyFeature.feature
Project-1
|-src
|-test
|-java
|-mypackage
|-MyFeatureTest.java
I tried giving "#CucumberOptions(features = "Project-2/resources/MyFeature.feature" ) but it is throwing NoSuchMethodError.
Can somebody please help!!
Add Project-1 dependency on Project-2 i.e. at runtime the test classes of Project-1 should be available in the classpath when you execute the cucumber tests.
Check mvn dependency:tree output and look for cucumber dependencies. All the dependencies are of same version i.e. cucumber-core, cucumber-junit, etc.
Bhushan Please click the link for Project Folders Structure

How to add Idea module to dependencies for gradle

I've a gradle module called "Service" and module called ProtocolLibrary, generated by Idea. How can I add library module to my main module?
I have tryed to simply add source dir to project:
main {
java.srcDirs = ['src/main/java', '../ProtocolLibrary/src']
resources.srcDirs = ['src/main/resources']
}
But it doen't work:
Error:Can't register given path of type 'SOURCE' because it's out of content root.
If you wish to combine two separate gradle projects together, then you should look at gradle multi-project builds. You will need to create a root project, which will include both your modules as subprojects and in that case, you can simply use one project as the dependency of another, like:
//service project dependencies
dependencies {
compile project(':ProtocolLibrary')
...
}
After doing this, there is no reason to include sources from one project into another.
The second approach is to use the artifact, generated by ProtocolLibrary. You can make a dependency in Service project specifying the jar in filesystem, or your ProtocolLibrary could be published in your maven repo and be used as usual dependency.

Categories

Resources