I am using Gradle to run my integration test. I am using JUnit for tests. Integration tests are not being executed when a Gradle task is being triggered.
I am using JUnit version 4.12 and Gradle version 3.5.
I am migrating a project from Ant build to Gradle. it executes all the integration tests when ant task is being triggered. I need to replace the build.xml file by build.gradle to execute all the integration test. This is the code in build.gradle file
apply plugin: 'java'
repositories {
mavenCentral()
}
sourceSets {
test {
java {
srcDirs = ['src/main/java']
}
resources {
srcDirs = ['src/test']
}
}
}
dependencies {
testCompile 'junit:junit:4.12'
testRuntimeOnly 'junit:junit:4.12'
compileClasspath sourceSets.main.output + sourceSets.test.output
runtimeClasspath sourceSets.main.output + sourceSets.test.output
}
task run(type: Test) {
useJUnit()
}
I am using another build.gradle file to copy the resources and code dependency to build/classes folder. code for this is:
distributions {
distZip.enabled = false
main {
baseName = 'project name'
version = ''
contents {
from { 'build.gradle' }
from { '../config/gradle/common-build.gradle' }
from {'../gradle*'}
from { 'build/libs/*.jar' }
into('build/classes') {
from('build/classes')
}
into('build/resources') {
from('build/resources')
}
}
}
}
i am using apply to import one build.gradle file into another using this code:
apply from: 'buildFileLocation/build.gradle'
But whenever I execute the task I am getting this as output:
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :run NO-SOURCE
Related
The assemble task includes the following tasks, as far as I know:
:compileJava
:processResources
:classes
:jar
:assemble
My jar task depends on a copy library task in which I copy all my libraries from my implementation configuration into a libs folder.
To get a working executable jar currently I assemble my project, execute the copy libs task and then the jar task again (which iterates over the libs and adds their name to the classpath) as this works fine.
:compileJava
:processResources
:classes
:jar
:assemble
:copyLibs
:jar
But when I put the copy libs task before the jar task via dependsOn then when I execute my project the jar doesn’t find all libs, even though it seems like they have been copied successfully.
:compileJava
:processResources
:classes
:copyLibs
:jar
:assemble
EDIT: The copyLibs and jar task of my build.gradle
project.configurations.implementation.setCanBeResolved(true)
task copyLibs(type: Copy) {
from configurations.implementation
into "$buildDir/libs"
}
build.dependsOn copyLibs
jar {
manifest {
attributes 'Main-Class': 'dc.v077a.server.ServerMain',
'Class-Path': fileTree("$buildDir/libs").filter {
it.isFile() && !it.name.startsWith("dc-v077a-server") }.files.name.join(' ')
}
}
Solved it by generating one fat executable jar that includes all dependencies.
That is not exactly the same but in my case it is exactly what i needed:
project.configurations.implementation.setCanBeResolved(true)
/*
* Generate a fat executable jar that includes all implementation dependencies
*/
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes 'Main-Class': 'dc.v077a.server.ServerMain'
}
from {
configurations.implementation.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
I'm using Spek testing framework for my Kotlin project. I am able to run tests through Intellij Idea Spek plugin, but can't run them through gradle (build or test). According to SimpleTest.kt when running from Idea plugin 1 test succeeded and 1 failed, when running via gradle it says that 1 container found with 0 tests. How to set up the launch of tests via gradle?
My gradle and test files:
build.gradle:
buildscript {
ext.kotlin_version = '1.1.4-2'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4"
}
}
apply plugin: "idea"
apply plugin: "java"
apply plugin: "kotlin"
apply plugin: "application"
apply plugin: "org.junit.platform.gradle.plugin"
junitPlatform {
filters {
engines {
include 'spek'
}
}
}
mainClassName = "app.MainKt"
repositories {
mavenCentral()
jcenter()
maven { url "http://dl.bintray.com/jetbrains/spek" }
}
dependencies {
testCompile 'org.jetbrains.kotlin:kotlin-test'
testCompile 'org.jetbrains.spek:spek-api:1.1.2'
testCompile 'org.junit.platform:junit-platform-runner:1.0.0-M4'
testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.2'
}
sourceSets.test.java.srcDirs += 'src/test/kotlin'
src/test/kotlin/SimpleTest.kt:
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import kotlin.test.assertEquals
class SimpleTest : Spek({
given("simple test") {
it("should succeed") {
assertEquals(1, 1)
}
it("shouldn't succeed") {
assertEquals(0, 1)
}
}
})
Gradle test output:
Executing external task 'test'...
Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set. This behaviour has been deprecated and is scheduled to be removed in Gradle 5.0
:generateBuildConfig UP-TO-DATE
:compileBuildConfig UP-TO-DATE
:extractIncludeProto UP-TO-DATE
:extractProto UP-TO-DATE
:generateProto UP-TO-DATE
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestKotlin
Using kotlin incremental compilation
:extractIncludeTestProto UP-TO-DATE
:extractTestProto UP-TO-DATE
:generateTestProto NO-SOURCE
:compileTestJava NO-SOURCE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:junitPlatformTest
Test run finished after 5062 ms
[ 1 containers found ]
[ 0 containers skipped ]
[ 1 containers started ]
[ 0 containers aborted ]
[ 1 containers successful ]
[ 0 containers failed ]
[ 0 tests found ]
[ 0 tests skipped ]
[ 0 tests started ]
[ 0 tests aborted ]
[ 0 tests successful ]
[ 0 tests failed ]
:test
:test SKIPPED
BUILD SUCCESSFUL in 14s
12 actionable tasks: 2 executed, 10 up-to-date
External task execution finished 'test'.
This is due to this issue with compatibility between Spek 1.1.2 and Kotlin 1.1.4 https://github.com/JetBrains/spek/issues/248
This can be resolved by using Spek 1.1.4 and junit-platform 1.0.0-RC2
I am writing the gradle script where I need to download the multiple jar files from artifactory I am using the apply plugin 'java' in gradle script and able to fetch those jar files from artifactory easily but if I apply this plugin it run the jar task automatically and creates the jar which I don't want.Is there a way to download the jar files from artifactory with/without using the java plugin so that jar task couldn't trigger.
apply plugin 'java'
apply plugin: 'base'
//-- set the group for publishing
group = 'com.abcdef.covery'
/**
* Initializing GAVC settings
*/
def buildProperties = new Properties()
file("version.properties").withInputStream {
stream -> buildProperties.load(stream)
}
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.coveryadBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.scoveryadBuildVersion
println "${version}"
//name is set in the settings.gradle file
group = "com.abcdef.discovery"
version = buildProperties.coveryadBuildVersion
println "Building ${project.group}:${project.name}"
repositories {
maven {
url "http://art.tsh.tho.com:90000/artifactory/services"
}
}
dependencies {
runtime "covery.services:AddService:1.0#jar"
runtime "covery.services:AddService:1.1#jar"
runtime "covery.services:Services:1.0#jar"
runtime "covery.services:Services:1.1#jar"
}
task copyDeps(type: Copy) {
from configurations.runtime
into 'services/discovery/services/'
}
output:Below shows few tasks which are running while using the java plugin in the script and if I don't use then it doesn't download the jar files from artifactory.
16:28:32 Building com.abcdefgh.discovery:cdad
16:28:33 [buildinfo] Properties file found at 'C:\Windows\TEMP\buildInfo429617857022686528.properties'
16:28:35 :copyDeps UP-TO-DATE
16:28:36 :deletebuild
16:28:37 :buildreportZip
16:28:38 :deleteGraphicsAssets
16:28:47 :unzip
16:28:47 :compileJava UP-TO-DATE
16:28:47 :processResources UP-TO-DATE
16:28:47 :classes UP-TO-DATE
16:28:47 :jar
16:28:47 :artifactoryPublish
16:28:47 Deploying artifact: http://localhost:8081/artifactory/libs-release-local/com/abcdefg/covery/cdad/03_00_00_183/cdad-03_00_00_183.zip
16:28:53 Deploying artifact: http://localhost:8081/artifactory/libs-release-local/com/abcdefl/covery/cdad/03_00_00_183/cdad-03_00_00_183.jar
You do not need to add the java plugin to download dependencies. If you do not have any java source files I would recommend that you use the baseplugin instead:
apply plugin: 'base'
repositories {
mavenCentral()
}
configurations {
nameOfMyConfiguration
}
dependencies {
nameOfMyConfiguration 'org.scala-lang:scala-library:2.10.4'
}
task zipMyStuff(type: Zip) {
from configurations.nameOfMyConfiguration
}
I have an IntelliJ idea project which has a number of dependencies in build.gradle.
However just recently after adding dependencies to buid.gradle, IntelliJ doesn't seem to be aware of them (despite Gradle building and running the applicaition fine).
My build.gradle:
apply plugin: 'java'
version = '1.0'
repositories {
mavenLocal()
maven { url * } // Company's proxy maven repository
}
dependencies {
compile("wsdl4j:wsdl4j:1.6.2")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.0-milestone-4'
distributionUrl = * // Company gradle distrubution URL
}
apply plugin: 'application'
mainClassName = "main.HelloWorld"
But when I use a class that is dependant, for example: javax.wsdl.xml.WSDLReader, IntelliJ looks like this
Despite it compiling and running fine.
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:run
Hello, World
BUILD SUCCESSFUL
How do I make IntelliJ aware of the classes?
I managed to fix this myself:
Under the toolbar I went View->Toolbars->Gradle. Then a sub window opened on the right. I then clicked on the refresh button, shown here:
And after a few moments is was all good!
When I try running gradle test, I get the following output:
$ gradle test
:ro:compileJava UP-TO-DATE
:ro:processResources UP-TO-DATE
:ro:classes UP-TO-DATE
:ro:jar
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
ro.idea.ToggleTest > testIsAd FAILED
java.lang.NoClassDefFoundError at ToggleTest.java:13
Caused by: java.lang.ClassNotFoundException at ToggleTest.java:13
ro.idea.ToggleTest > testToggle FAILED
java.lang.NoClassDefFoundError at ToggleTest.java:13
2 tests completed, 2 failed
:test FAILED
So I want to check my classpath to see whether my classpath is wrong or not.
My question is: How can I list the classpath at test time with a Gradle task?
You can list test runtime dependencies with:
gradle dependencies --configuration=testRuntime
Or, if you want to see the actual files:
task printClasspath {
doLast {
configurations.testRuntime.each { println it }
}
}
Also, to list the classpath for the main (non-test) application run use:
run << {
doLast {
configurations.runtime.each { println it }
}
}
Worked for me (Gradle 6.3, Kotlin DSL):
tasks.withType<Test> {
this.classpath.forEach { println(it) }
}
this works for me (in Gradle 5.6)
task printTestClasspath.doLast {
println(sourceSets.test.runtimeClasspath.asPath)
}
Assuming you are using a Gradle wrapper, you can use the following.
./gradlew dependencies --configuration=testRuntimeClasspath
It will list the dependencies as available to your tests.
Expanding solution of Peter Niederwieser, if you want to print from all possible configurations (using the simple loop) and ignoring possible errors:
task printClasspath {
doLast {
configurations.each { Configuration configuration ->
try {
println configuration
configuration.each { println it }
} catch (Exception e){
println "Error getting details of $configuration"
}
}
}
}
I'm using gradle 7.3.3. and this worked for me:
tasks.withType<Test> {
useJUnitPlatform()
println("Test classpath")
sourceSets.test.get().runtimeClasspath.forEach { println(it) }
}