How to configure with gradle running JUnit4 tests with categories using vintage engine of jupiter Junit5 platform - junit4

When we had only JUnit4 tests, we used
test {
useJUnit{
excludeCategories "SlowTests"
}
}
Now migrating to JUnit5 and still having JUnit4 tests around, how do we use it in JUnitPlattform to let tests marked with a JUnit test category out?
test {
useJUnitPlatform{
// ???
}
}

JUnit4's categories are mapped to tags according to the full class name of the category
So the following should work:
tasks.test {
useJUnitPlatform {
includeTags.add("com.acme.Example")
// or the following to exclude the tag: includeTags.add("!com.acme.Example")
}
}

Related

Replacement for ScalaCompile in gradle when switching to Kotlin

I convert a project that was originally written in Scala into kotlin. It is a typical backend - frontend-design with the backend written in Scala and the Frontend in Angular (the code was written by a former co-worker who is no longer working in this project)
During the gradle-build, the e2e-tests are evaluated. For this purpose the backend is compiled in the following way:
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.scala.ScalaCompile
class ScalaCompilerPlugin implements Plugin<Project> {
void apply(Project project) {
project.configurations {
scalaCompilerPlugin
}
project.afterEvaluate {
project.tasks.withType(ScalaCompile) {
options.encoding = 'UTF-8'
if (scalaCompileOptions.additionalParameters == null) {
scalaCompileOptions.additionalParameters = []
}
scalaCompileOptions.additionalParameters <<
"-Xplugin:" + project.configurations.scalaCompilerPlugin.asPath
}
}
}
}
The 'scalaCompilerPlugin'-plugin is then added to the build.gradle-file in the backend.
plugins {
id 'scala'
id 'com.github.psxpaul.execfork' version '0.1.12'
}
apply plugin: ScalaCompilerPlugin
// ...
Since now the project is converted to Kotlin/Ktor I plan to use something like a KotlinCompiler-plugin for the same purpose, but in gradle 6.0.1, there seems to be no such plugin, and not being that firm with gradle I don't know how to add org.jetbrains.kotlin.gradle.tasks.KotlinCompile.
How can I achieve the same and start the backend during the gradle-build to run the e2e-tests during the test-runs? I am also open to suggestions to change this design.

Eclipse, Buildship, Jigsaw: java.lang.module.FindException

Considering that Java 1.9 is now out for some time, i am still amazed how difficult it is to get a 1.9 project up and running with eclipse and buildship.
I now spend 2 days trying to get this flying and came very far but one issue is something i just get not off the ground:
"Right click on JUnit test case and 'Run as...'".
It always ends in a java.lang.module.FindException and i have to override the command line as described in Eclipse cannot find module even the module path is explicitly provided.
On top comes this whole "eclipse.classpath.file.whenMerged" stuff in the build.gradle file (my file see below).
Hence my question is two-fold:
Have i missed something that makes the jigsaw support with Gradle and buildship more easy?
What do I have to do to not patch the command line manually (as described in the link above)?
As I am currently evaluating the support of Eclipse/Gradle/Buildship for my team I need to have the second question fixed. Otherwise the developer in my team are going to get nuts. As such I am also contemplating to move away from Eclipse.
Side question: How good is the support in other IDE for modules and Gradle?
I am using
Eclipse 2020-12 (4.18.0.v20201202-1800)
Eclipse Plug-ins for Gradle 3.1.4.v20200326-1743
Java 12
Grade 6.7.1
Thank you
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java library project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.7.1/userguide/building_java_projects.html
*/
plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
id 'eclipse'
id 'application'
}
sourceCompatibility = targetCompatibility = '1.12'
repositories {
// Use JCenter for resolving dependencies.
jcenter()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'org.postgresql:postgresql:42.2.18'
}
java {
modularity.inferModulePath = true
}
application {
mainModule = 'com.myproduct.mod.lib' // name defined in module-info.java
mainClass = 'com.myproduct.mod.lib.Library'
}
eclipse {
classpath {
file {
whenMerged {
//Define a module as being either a library or a project dependency.
//Test sources are excluded because eclipse wants them on the classpath for some reason (1)
entries.findAll { (it instanceof org.gradle.plugins.ide.eclipse.model.Library || it instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) && !it.entryAttributes.get('gradle_used_by_scope').equals('test') }.each {
it.entryAttributes['module'] = 'true'
}
//Test-scoped stuff should set the appropriate flag
entries.findAll { (it.properties.kind.equals('src') || it.properties.kind.equals('lib')) && it.entryAttributes.get('gradle_used_by_scope').equals('test') }.each {
it.entryAttributes['test'] = 'true'
}
entries.findAll { isConGradle(it) }.each {
it.entryAttributes['module'] = 'true'
}
}
}
}
}
boolean isConGradle(entry) {
entry.kind == 'con' && entry.path == 'org.eclipse.buildship.core.gradleclasspathcontainer'
}

Using sbt Avrohugger plugin in gradle

I am using https://github.com/julianpeeters/avrohugger sbt plugin to generate Scala case classes for Avro .avsc files. How can I use the same plugin in a Gradle project?
I created gradle plugin for generating Scala case classes from Avro schema and it internally uses avrohugger library.
So now it's enough to add this plugin to your project:
plugins {
id 'com.zlad.gradle.avrohugger' version '0.2.1'
}
When you want to compile Scala sources you need Scala library dependency too:
plugins {
id 'scala'
id 'com.zlad.gradle.avrohugger' version '0.2.1'
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.12.8'
}
Scala classes will be automatically generated during build before compileScala task. By default avro schema should be in src/main/avro and generated sources will be in build/generated-src/avro
You can call generateAvroScala to invoke Scala sources generation manually.
You can find all details and configuration options on gradle-avrohugger-plugin github page.
I ended up using the avrohugger-tools library in Gradle to autogenerate Scala case classes whenever my schemas were updated. Your mileage may vary, but this finally worked for me:
build.gradle.kts
import java.io.File
plugins {
scala
id("com.github.maiflai.scalatest") version "0.19"
}
version = "1.0"
configurations {
register("avrohugger-tools")
}
dependencies {
// Scala std-libs
implementation(Dependencies.Libs.Scala.library)
// AvroHugger tools JAR
"avrohugger-tools"("com.julianpeeters:avrohugger-tools_2.12:1.0.0-RC14")
// testing
testImplementation(gradleTestKit())
testImplementation("junit:junit:4.12")
testImplementation("org.scalatest:scalatest_2.12:3.0.5")
testRuntime("org.pegdown:pegdown:1.4.2")
}
fun normalizeFileSeparator(path: String) = path.replace('/', File.separatorChar)
val genAvro = task<JavaExec>("genAvro") {
val sourceAvroPath = normalizeFileSeparator("src/main/avro/")
val sourceAvroFiles = fileTree(mapOf(
"dir" to sourceAvroPath, "include" to listOf("**/*.avsc")
))
val targetPath = normalizeFileSeparator("src/gen/")
doFirst {
delete(targetPath)
}
classpath = configurations["avrohugger-tools"]
main = "avrohugger.tool.Main"
args(
"generate", "schema",
sourceAvroFiles.files.joinToString(separator=" "),
targetPath
)
inputs.files(sourceAvroFiles)
outputs.files(targetPath)
}
the<JavaPluginConvention>().sourceSets.getByName(
SourceSet.MAIN_SOURCE_SET_NAME
).java.srcDir("gen")
tasks.withType<ScalaCompile> {
dependsOn(genAvro)
}
inline fun <reified T : Task> TaskContainer.existing() = existing(T::class)
inline fun <reified T : Task> TaskContainer.register(name: String, configuration: Action<in T>) = register(name, T::class, configuration)
Note that I'm also building/test a Scala source tree in this package, so some of the Scala-specific pieces can be presumably elided.
Really hope this helps!

Spring Boot MongoDb QueryDSL Gradle Intellij Integration for generating the QueryDSL Q classes

I am trying to generate the QueryDSL Q classes for my Mongo entities using gradle in a Spring Boot project. The IDE I'm using is Intellij.
The code I'm using is adapted from this topic Generating JPA2 Metamodel from a Gradle build script:
sourceSets {
generated {
java {
srcDirs = ['src/generated/java']
}
}
}
configurations {
querydslapt
}
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.compilerArgs = [
"-proc:only",
"-processor", "org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor"
]
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}
compileJava {
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
}
compileGeneratedJava {
dependsOn generateQueryDSL
options.warnings = false
classpath += sourceSets.main.runtimeClasspath
}
clean {
delete sourceSets.generated.java.srcDirs
}
idea {
module {
downloadJavadoc = true
downloadSources = true
generatedSourceDirs += file('src/generated/java')
}
}
The problem is that in the end in Intellij I have 3 modules. Main, test and generated. The test and generated modules are dependent on the main module. I would like also for the main module to be dependent on the generated module since I'm using the generated Q classes in my code.
All my attempts to solve this end up in a cyclic dependency error from Gradle.
Can someone give me some tips what I could try to solve this problem.
Thanks!

No JUnit tests found in Eclipse using Groovy + Cucumber

I have a Cucumber test in Groovy as follows
import org.junit.runner.RunWith
import cucumber.junit.Cucumber
import cucumber.junit.Cucumber.Options
#RunWith(Cucumber.class)
#Options(features = ["classpath:CarDrive.feature"])
public class FuelCarTest {
}
import cucumber.annotation.en.Given
import cucumber.annotation.en.Then
import cucumber.annotation.en.When
public class FuelCarSteps {
public FuelCarSteps() {
println "FuelCarSteps::FuelCarSteps"
}
#Given("I have a car")
def givenCar() {
println "I have a car"
}
#When("^you fill it with 50 litres of fuel")
def addFuel() {
println "add fuel"
}
#Then("^the tank contains 60 litres")
def checkBalance() {
println "TODO: add check here"
}
}
I can run the test fine using mvn test, but when I try to run it in Eclipse I get
No JUnit tests found
Tried cleaning, rebuilding & restarting
Hi you need to create run configuration for the each test in junit
Provide full path ( like package1.subpackege1.testclassname ) in class input in run config
Control, if you have any errors in the Groovy code, such as Type of expression is statically unknown: or similar. They don't show off as usual errors and could be almost invisible.