Gradle can't find tests - java

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

Related

Running integration test in Gradle using junit

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

Eclipse - Java - Gradle is skipping jacocoTestReport

Project structure:
src/main/java
src/main/resources
src/test/java
Gradle version : 2.2.1
Here is my build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'jacoco'
version = '1.0'
sourceCompatibility = 1.7
targetCompatibility = 1.7
test {
include 'src/test/java'
finalizedBy jacocoTestReport
}
jacoco {
toolVersion = "0.7.6.201602180812"
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/reports/jacoco/html"
}
}
when I run gradle task as "test jacocoTestReport", I am getting the below results
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:jacocoTestReport SKIPPED
can someone please suggest what should be added to execute jacoco test report.
Thanks.
The task will only run if coverage data is available. You can make sure of that by also running the test task.
Source - Running jacocoReport
I was able to generate the code coverage results with the following set up.
apply plugin: 'jacoco'
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
You can force it to run with:
jacocoTestReport {
onlyIf = {
true
}
}
This will probably give an error (there's a reason it didnt run in the first place), but the error should give more information.
Unfortunately, none of these answers worked for me.
For Spring 2.5 Users, who got stuck with it for hours -just like myself.
I had a similar issue.
I was not having the exec file generated.
And because of that ,
I found that the jacocoTestReport was simply "skipped".
I got it fixed by adding :
test {
useJUnitPlatform()
finalizedBy jacocoTestReport // report is always generated after tests run
}
That's because I'm using Junit5 with spring boot 2.X - Gradle 7.1
And as of today Junit5 is not invoked by default in the test task.
Late at party, but none of the answers above solved for me. Instead, changing
dependencies {
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}
to
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}
worked like a charm (source).

Can not publish Java project to Maven via BinTray with Gradle

I have a simple project I'm trying to publish to Maven via BinTray, but have been getting an error.
I have followed a guide to publish to bintray, and appear to have BinTray all set up, as well as access to Sonatype. Signing/etc, all appears to be good.
When I run "./gradlew bintrayUpload" I get an error, but the artifacts do show up on bintray. However I get various POM errors.
adams-MBP:UsedUtil adamhammer2$ ./gradlew clean install
:clean
:compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.7
Note: /Users/adamhammer2/git/UsedUtil/src/main/java/com/mysaasa/usedutil/CallKeyGenerator.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
:processResources UP-TO-DATE
:classes
:jar
:javadoc
:javadocJar
:sourcesJar
:install
BUILD SUCCESSFUL
Total time: 7.233 secs
adams-MBP:UsedUtil adamhammer2$ ./gradlew bintrayUpload
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:javadoc UP-TO-DATE
:javadocJar UP-TO-DATE
:sourcesJar UP-TO-DATE
:install
:bintrayUpload FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bintrayUpload'.
> Could not upload to 'https://api.bintray.com/content/adamhammer/maven/used-util/0.9.1/com/mysaasa/used_util/UsedUtil/0.9.1/UsedUtil-0.9.1.pom': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'com/mysaasa/used_util/UsedUtil/0.9.1/UsedUtil-0.9.1.pom']
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 12.923 secs
Github project is https://github.com/ahammer/UsedUtil
build.gradle file is
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
apply plugin: 'java'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
ext {
bintrayRepo = 'maven'
bintrayName = 'used-util'
publishedGroupId = 'com.mysaasa.used_util'
libraryName = 'UsedUtil'
artifact = 'usedutil'
libraryDescription = 'A Library for tracking usage in java projects'
siteUrl = 'http://ahammer.github.io/UsedUtil'
gitUrl = 'https://github.com/ahammer/UsedUtil.git'
libraryVersion = '0.9.1'
developerId = 'adamhammer'
developerName = 'Adam Hammer'
developerEmail = 'adamhammer2#gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
After publishing I get the error, however in BinTray it registers the upload. When I click add to JCenter however I get another error "- Add a POM file to the latest version of your package."
Instead of using the 3rd party script and out of date plugin, I followed the guide
here https://github.com/bintray/gradle-bintray-plugin
This generated a proper pom file and published to bintray and made my package compatible with jcenter.
For a working build.gradle, you can look at my github for the working version.

IntelliJ 14 Java project not aware of Gradle dependancy

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!

Followup to Gradle 1.3: build.gradle not building classes

I am actually using the newly released Gradle 2, but having the same issues as described in the previous post.
I am also a newb trying to follow the example given in the Spring guide (http://spring.io/guides/gs/gradle/#scratch) but after my first compile, there were no classes.
I have tried various configurations of tree structure including adding the structure and code suggested in the above thread:
"I guess the source file path is src/org/gradle/example/simple/HelloWorld.java. (The diagram shows something different.) That doesn't match Gradle's default, so you'll have to reconfigure it: sourceSets.main.java.srcDirs = ["src"] – Peter Niederwieser Dec 7 '12 at 1:23 "
adding the line: sourceSets.main.java.srcDirs = ["src"] allows the code to compile, however, I still have no classes.
Here is the successful build.
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
BUILD SUCCESSFUL
Total time: 4.468 secs
Here is the build file:
apply plugin: 'java'
sourceSets.main.java.srcDirs=["src"]
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile "joda-time:joda-time:2.2"
}
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
task wrapper(type:Wrapper) {
gradleVersion = '1.11'
}
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
Where are my classes? Please help.
After I got stuck with the same problem, I hacked around for a bit before I understood the reason for this behavior.
My project structure was like so
MyProject
- src
- main
- java
- hello
HelloWorld.java
build.gradle
The problem was that the build.gradle is supposed to be under the Project-Root folder i.e. MyProject and not under the hello folder !!!
Changed it so that my Project structure looks like below, ran the gradle build and saw that classes folder was created:
MyProject
- src
- main
- java
- hello
HelloWorld.java
build.gradle
When you think about it, the build.gradle is used to build the complete project and not just the classes within one folder and should rightfully sit under the project-root folder.

Categories

Resources