I'm trying to setup unit tests in the new LibGdx project.
I added dependencies to :core project:
project(":core") {
apply plugin: "kotlin"
apply plugin: 'kotlin-kapt'
dependencies {
// ...
testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.1"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.6.1"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
kaptTest "org.mockito:mockito-core:3.3.3"
kaptTest "com.google.dagger:dagger-compiler:$daggerVersion"
}
Test folder: core/src/test/kotlin/
Test class:
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
class FirstTest {
#Test
fun `my first test`() {
assertEquals(1,1)
}
}
But I got error: Unresolved reference: junit
What I'm doing wrong?
P.S.: I'm trying run tests in IntelliJ IDEA
[ EDIT ]
Link to repo test branch: https://github.com/Alcadur/libgdx-not-working-tests
Finally, I found the solution for my problem (full article)
I have to replace
sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.test.java.srcDirs = [ "test" ]
to
sourceSets.main.java.srcDirs = [ "src/main/java/" ]
sourceSets.test.java.srcDirs = [ "src/test/java/" ]
I also update repo for this question: GitHub repo
Related
I tried to implement Kotlin check style in project using ktlint.
I added
plugins {
id("org.jlleitschuh.gradle.ktlint") version "10.1.0" apply false
}
in root build.gradle.kts and
plugins {
id("org.jlleitschuh.gradle.ktlint")
}
in build.gradle.kts in subprojects
When I called
gradlew ktlintCheck
I got
FAILURE: Build failed with an exception.
* What went wrong:
org/jetbrains/kotlin/gradle/dsl/KotlinProjectExtension
How can I fix it?
This is how I went about it for Klint with Kotlin DSL on gradle.kts files for Android Gradle. Have also added Spotless with Kotlin DSL ( code formatter ) at the same time.
It is also worth noting mine is NOT a Multi Module project.
Step 1 - gradle.kts (app) - Add Klint and Spotless plugins and import Reporters
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
plugins {
id("com.android.application") ...
//Klint
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
//Spotless
id("com.diffplug.spotless")
}
Step 2 - gradle.kts (app) - Configure Klint only
// ktlintFormat task will need to run before preBuild
tasks.getByPath("preBuild")
.dependsOn("ktlintFormat")
configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
android.set(true)
ignoreFailures.set(false)
disabledRules.set(setOf("final-newline", "no-wildcard-imports"))
reporters {
reporter(ReporterType.PLAIN)
reporter(ReporterType.CHECKSTYLE)
reporter(ReporterType.SARIF)
}
}
Step 3 - gradle.kts(app) - Configure Spotless only
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
// version, setUseExperimental, userData and editorConfigOverride are all optional
ktlint("0.45.2")
.setUseExperimental(true)
.userData(mapOf("android" to "true"))
.editorConfigOverride(mapOf("indent_size" to 2))
}
kotlinGradle {
target("*.gradle.kts") // default target for kotlinGradle
ktlint() // or ktfmt() or prettier()
}
}
Step 4 - gradle.kts(project) - Add Spotless in the Classpath
buildscript {
repositories{
google()
mavenCentral()
}
dependencies {
...
//class path for Spotless
classpath("com.diffplug.spotless:spotless-plugin-gradle:6.9.1")
}
}
The 2 plugins works fine with this set-up.
...obviously some necessary things are not included from the dependencies.
Once it reaches a call to an external library, it breaks, either with ClassNotFoundException, or without a word.
I started with this skeleton project.
Relevant changes in build.gradle:
application {
mainClassName = 'net.laca.FoKt'
}
(my main function is in fo.kt)
dependencies {
//...
compile "com.sparkjava:spark-core:2.9.3"
implementation 'com.google.code.gson:gson:2.8.6'
implementation fileTree('libs') { include '*.jar' }
}
jar {
archiveBaseName = 'csira'
// Uncommend the last two lines to build a "fat" jar with `./gradlew jar`,
// and run it without Gradle's help: `java -jar build/libs/skeleton.jar`
manifest { attributes 'Main-Class': 'net.laca.FoKt' }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
versions: Kotlin 1.4.20, Java 11, Gradle 6.7.1
Allegedly it should work this way. As it does if I start it with gradle run.
But when I start it with java -jar build/libs/csira.jar after gradle jar, it doesn't.
Relevant parts of fo.kt:
package net.laca
import spark.Spark.*
import com.google.gson.GsonBuilder
fun main(args: Array<String>) {
before("/*")
{ req, res ->
res.type("application/json")
println("hívás: ${req.requestMethod()} ${req.pathInfo()} " + req.queryString())
println(GsonBuilder().create().toJson(req.queryMap().toMap())) //line 14
//...
}
At GsonBuilder it breaks:
java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder
at net.laca.FoKt$main$1.handle(fo.kt:14)
at spark.FilterImpl$1.handle(FilterImpl.java:73)
at spark.http.matching.BeforeFilters.execute(BeforeFilters.java:48)
at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:133)
at ...
...
Caused by: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 19 more
And when I take/comment out the 14th line, and it reaches a call to my own jar in /libs:
get("/whatever")
{
println("before")
com.zz.app.MyScalaClass.apply().myFun()
println("after")
}
then the last thing I see is before, the rest is silence.
It happens because your jar task is configured incorrectly. To understand why, look at your dependencies:
dependencies {
//...
compile "com.sparkjava:spark-core:2.9.3"
implementation 'com.google.code.gson:gson:2.8.6'
implementation fileTree('libs') { include '*.jar' }
}
You are using both the compile and implementation configurations. The former is deprecated and should not be used by the way.
Then look at the jar task:
jar {
archiveBaseName = 'csira'
// Uncommend the last two lines to build a "fat" jar with `./gradlew jar`,
// and run it without Gradle's help: `java -jar build/libs/skeleton.jar`
manifest { attributes 'Main-Class': 'net.laca.FoKt' }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
The from part instructs Gradle to collect all dependencies from the compile configuration only, and this will ignore the implementation configuration completely.
While you could change "compile" to "implementation" everywhere, the correct way to construct a fat jar is to actually collect from the runtimeClasspath configuration. This one extends other configurations like compile and implementation, but also runtimeOnly which you might find handy in the future.
There is actually also an example of how to do this in the Gradle user guide. To adapt it for your project, it should look like:
jar {
archiveBaseName = 'csira'
manifest { attributes 'Main-Class': 'net.laca.FoKt' }
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
}
The extra dependsOn line ensures that the runtimeClasspath configuration is completely resolved before trying to use it. Another difference is that it only collects jar files.
I am trying to share Java code between Android and JVM targets using Kotlin Multiplatform feature (sample project: https://github.com/dmitrykolesnikovich/accessJavaCode-issue)
Simply saying, ":library1" and ":library2" both are Kotlin multiplatform libraries targeting JVM and Android. ":library2" depends on ":library1". They both uses Kotlin and Java. ":library2" is intended to be dependency of 1) Android application and 2) desktop (JavaFX) application. That's why 1) AAR artifact and 2) JAR artifact both are needed (?) - so I use 1) Android target and 2) JVM target for both ":library1" and ":library2".
The problem is that, when I have Java code in ":library1"
public class JavaCode {} // JavaCode.java
And Kotlin code in ":library2" that depends on ":library1"
class AccessJavaCode : JavaCode() // AccessJavaCode.kt
Android target is OK with recognizing Java but JVM target is not:
> Task :library2:compileKotlinJvm FAILED
e: AccessJavaCode.kt: (3, 38): Unresolved reference: JavaCode
In gradle config I define two plugins: kotlin-multiplatform and com.android.library:
apply plugin: "kotlin-multiplatform"
apply plugin: "com.android.library"
kotlin {
targets {
jvm()
android()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
}
}
androidMain {
dependsOn jvmMain
}
}
}
android {
compileSdkVersion 28
sourceSets {
main {
java.srcDirs += "src/jvmMain/kotlin" // Android target recognizes Java with this
manifest.srcFile "src/androidMain/AndroidManifest.xml"
}
}
}
I am pretty sure it’s something simple with my gradle file. Many thanks for your help guys.
~~~~ EDIT ~~~~
Another workaround the issue without losing the ability to generate an android archive .aar for the library1 would be to make a new version of this same library depending on precompiled artefacts of a splitted version of the original library1.
So you would end up with a multi-module gradle project, something like this:
library1-jvm with java plugin enabled
library1-android with android plugin enabled
library1 which will depends on prebuilt library1-jvm.jar and library1-android.aar
You could use whatever you prefer to publish those artefacts, but a local maven repo should work just fine!
That would mean replacing:
kotlin {
targets {
jvm()
android()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
}
}
androidMain {
dependsOn jvmMain
}
}
}
with:
kotlin {
targets {
jvm()
android()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
api "com.company:library-jvm:1.0.0"
}
}
androidMain {
dependsOn jvmMain
dependencies {
api "com.company:library-android:1.0.0"
}
}
}
}
That way, you don't need the java plugin at all in the final library1, because all the java code will already be built in a separate step.
Hence library1 could keep both the JVM and Android targets
~~~~~~~~~~~~~
In order to fix your issue, you need to:
Split your build.gradle configuration in order to have one config per library, this will be needed because you cannot enable the java plugin and the android at the same time for the same Gradle project, or you will end up with the following Error: The 'java' plugin has been applied, but it is not compatible with the Android plugins
Enable the java plugin in your library1 project if you want your JVM target to recognize your Java source files.
The Java source files need to be placed in the sibling directories java of the kotlin source roots.
More infos: Kotlin docs for java-support-in-jvm-targets
I also created a pull request solving your issue.
The downside of this approach is that you will not be able to generate an android archive .aar for the library1, but I guess using the java archive .jar in your android project should not be a problem at all.
library1/build.gradle:
apply plugin: "kotlin-multiplatform"
kotlin {
jvm {
withJava()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
}
}
}
}
library2/build.gradle:
apply plugin: "kotlin-multiplatform"
apply plugin: "com.android.library"
kotlin{
jvm()
android()
sourceSets {
jvmMain.dependencies {
api project(":library1")
}
androidMain {
dependsOn jvmMain
}
}
}
android {
compileSdkVersion 28
sourceSets {
main {
java.srcDirs += "src/jvmMain/kotlin"
manifest.srcFile "src/androidMain/AndroidManifest.xml"
}
}
}
I am trying to create a test class (JUnit 5) using Intellij Idea but I get the bellow error. When I created the test class it did NOT show the fix button so I am pretty sure the library is in class path.
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:45)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
My build.gradle looks as below:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
group 'ict221'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.11
mainClassName = 'boardgame.gui.RunGame'
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
javafx {
modules = [ 'javafx.controls', 'javafx.fxml' ]
version = "11.0.2"
}
Any help is much appreciate it.
As mentionned by #CrazyCode in comments, with Intellij you need to specify the version of JUnit (because Intellij is bundled with old version of JUnit)
Add this :
dependencies {
...
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.6.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.1")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.6.1")
}
try entering these depencies for junit5 in a gradle project
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")
Below is an example of a build.gradle for a simple application with JDK 17, Gradle 7.5.1 and JUnit Jupiter 5.9.0, which causes IntelliJ IDEA to correctly run unit tests with JUnit.
Notice that there is no need to add test dependencies as it is done automatically by configuring testing section.
build.gradle
plugins {
id 'application'
}
group = 'your.app'
version = '0.0.1'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
}
testing {
suites {
test {
useJUnitJupiter('5.9.0')
}
}
}
application {
mainClass = 'your.app.App'
}
src/main/test/your/app/AppTest.java
package your.app;
import org.junit.jupiter.api.Test;
class AppTest {
#Test
void testFeature() {
}
}
I am making a skeletton of a java project; the gradle build file has an annoying problem : tests are ran twice, one time by the task 'JUnitPlatformTest' and a second time by the task 'test'.
The first one seems to trigger the second, so I can't disable it, and I would like to keep the second one as there is a little difference between them : the first one is in the console (of intelliJ) and the second uses the integrated intelliJ window.
here is gradle.build
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
classpath 'eu.appsatori:gradle-fatjar-plugin:0.3'
}
}
plugins {
id 'java'
id 'edu.sc.seis.launch4j' version '2.4.4'
}
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'javafx-gradle-plugin'
apply plugin: 'eu.appsatori.fatjar'
junitPlatform {
platformVersion '1.0.3'
reportsDir file('build/test-results/junit-platform')
enableStandardTestTask true
//show results summary even on success.
details details.SUMMARY
filters {
tags {
// Framework tests need to be run only when required to verify that this framework is still working.
exclude "Framework"
}
includeClassNamePatterns '.*Test', '.*Tests'
}
}
group 'lorry'
version '1'
sourceCompatibility = 1.8
//mainClassName="imports.ColorfulCircles"
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
def final junitVersion = "5.2.0"
compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
//compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.11.0'
compile group: 'org.assertj', name: 'assertj-core', version: '3.9.0'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.7.22'
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
compile 'org.hamcrest:hamcrest-all:1.3'
testCompile "org.testfx:testfx-core:4.0.13-alpha"
testCompile 'org.testfx:testfx-junit5:4.0.13-alpha'
testRuntime 'org.testfx:openjfx-monocle:8u60-b27'
}
test {
useJUnitPlatform()
jvmArgs = [
"-Dtestfx.robot=glass",
"-Dtestfx.headless=true",
"-Dprism.order=sw",
"-Dprism.text=t2k",
"-Dheadless.geometry=1920x1200-32"
]
}
test.dependsOn 'clean'
jfx {
// minimal requirement for jfxJar-task
mainClass = 'imports.ColorfulCircles'
// minimal requirement for jfxNative-task
vendor = 'lolveley'
}
jar {
baseName = 'executable3'
version = ''
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'imports.ColorfulCircles'
)
}
}
launch4j {
outfile='bibliotek-v3.exe'
mainClassName = 'imports.ColorfulCircles'
icon = "${projectDir}\\icons\\hands2.ico"
copyConfigurable = project.tasks.fatJar.outputs.files
jar = "lib/${project.tasks.fatJar.archiveName}"
//headerType = "console"
//jar = "${buildDir}\\productFatJar\\fat.jar"
}
junitPlatformTest {
jvmArgs = [
"-Dtestfx.robot=glass",
"-Dtestfx.headless=true",
"-Dprism.order=sw",
"-Dprism.text=t2k",
"-Dheadless.geometry=1920x1200-32"
]
}
and here is the result:
Testing started at 19:25 ...
19:25:01: Executing task 'test'...
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :clean
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :junitPlatformTest
constructeur appelé
Before all
Before each
my test 1
Before each
my test 2
This test method should be run
Test run finished after 3630 ms
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 7 tests found ]
[ 0 tests skipped ]
[ 7 tests started ]
[ 0 tests aborted ]
[ 7 tests successful ]
[ 0 tests failed ]
> Task :test
constructeur appelé
Before all
Before each
my test 1
Before each
my test 2
This test method should be run
BUILD SUCCESSFUL in 13s
5 actionable tasks: 5 executed
19:25:15: Task execution finished 'test'.
According to this official website, ...
The JUnit Platform Gradle Plugin is deprecated
The very basic junit-platform-gradle-plugin developed by the JUnit team was deprecated in JUnit Platform 1.2 and will be discontinued in 1.3. Please switch to Gradle’s standard test task.
So you should remove this plugin from your build file and, if necessary, try to port the remaining settings to the test task of the java plugin.