Unable to resolve class org.gradle.util.TestUtil in Gradle - testing

This error means that class TestUtil is not on the classpath and the compiler can not find it. I came across such error hundred times before and there was missing Jar or wrong written class names, but now I just don't know what is wrong.
In my buildSrc dir I have custom Task and I made test for it:
package com.example.core.tasks;
import spock.lang.Specification
import org.gradle.api.Project
import org.gradle.util.TestUtil
public class GetInfoTaskTest extends Specification {
def "check files"(){
given:
def project = TestUtil.createRootProject()
when:
GetInfoTask getInfo = project.tasks.create("getInfo", GetInfoTask)
then:
getInfo instanceof GetInfoTask.class
}
}
In the build script in the buildSrc dir:
dependencies {
compile localGroovy()
testCompile 'org.spockframework:spock-core:0.7-groovy-1.8'
testCompile gradleApi()
}
Error:
GetInfoTaskTest.groovy: 4: unable to resolve class org.gradle.util.TestUtil
# line 3, column 1.
import org.gradle.util.TestUtil
^
I checked that this TestUtil is not internal. I still don't know why gradle can not find it.

TestUtil is an internal class. You can't use it in your own code.

Related

unresolved supertypes: javax.sql.DataSource

I am seeing the following error when trying to compile using gradle.
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class com.zaxxer.hikari.HikariDataSource, unresolved supertypes: javax.sql.DataSource
Adding -Xextended-compiler-checks argument might provide additional information.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
//kotlin("jvm") version "1.6.0"
}
group = "com.jasin"
version = "0.0.1"
dependencies {
//api(files("libs/jdbc-stdext-2.0.jar"))
implementation("org.ktorm:ktorm-core:3.4.1")
implementation("org.ktorm:ktorm-support-mysql:3.4.1")
implementation("mysql:mysql-connector-java:8.0.25")
implementation("com.zaxxer:HikariCP:5.0.1")
implementation("org.slf4j:slf4j-api:1.7.36")
implementation("org.slf4j:slf4j-simple:1.7.36")
testImplementation(kotlin("test"))
}
tasks.test {
useJUnit()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
If I uncomment the following line
//api(files("libs/jdbc-stdext-2.0.jar"))
I get the following type errors
module jdbc.stdext reads package javax.sql from both jdbc.stdext and java.sql
If I recomment this line and try to build again(without cleaning the project), it works. As soon as I clean the project the error reappears. What is exactly going on here and how can I fix this? Any help would be appreciated.
edit: I should add that this is a multi-module project and it wasn't till I tried to define module-info.java files in separate modules that this error appeared. But the error is specific to one module, the module that I use to access my database that uses connection pooling with hikaricp.
module-info.java file
module com.cobis.db {
requires kotlin.stdlib;
requires com.zaxxer.hikari;
requires ktorm.core;
exports com.cobis.db;
exports com.cobis.db.entities;
exports com.cobis.db.utilities;
}

Include a module as a dependency into a KMM project's shared module

I have a working KMM application, and I have a java module, mymodule, that I created with File->New->Module->Java or Kotlin library.
The module exists at the top level beside androidApp, iosApp and shared. In my settings.gradle.kts I have include(":mymodule").
I want to use mymodule in the shared module. So I go into the shared module's build.gradle.kts and I try to include my module in commonMain:
kotlin {
...
sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":mymodule"))
}
}
...
}
...
}
...
And the error is Could not resolve MyKMMApplication:mymodule:unspecified and:
Could not resolve project :mymodule.
Required by:
project :shared
Things I've tried
I can put dependencies { implementation(project(":mymodule")) } at the bottom of shared's build.gradle.kts and but still the same error appears
As to test if there's other problems, I can also import mymodule into the Android project without problems
I can include implementation("com.squareup.sqldelight:runtime:1.5.3") in commonMain and see those classes in the shared module no problem
The docs say you can include another multiplatform module, but nothing about a normal module.
How can I include a modules into KMM's shared module as a dependency?
So, the module you include into shared needs to be a multiplatform module. It's build.gradle.kts file should look something like this:
plugins {
id("org.jetbrains.kotlin.multiplatform")
}
kotlin {
jvm()
iosX64()
iosArm32()
iosArm64()
}
And it's project structure should look something like: mymodule/src/commonMain/kotlin/com/example/mymodule/.

Serialize kotlin data class in gradle's buildSrc/ during build

I'm looking to produce json files during the build cycle of my kotlin gradle app. My intent is to be able to instantiate data classes with a combination of public and private app configuration values that get put into the build's resources directory.
I'm looking at kotlinx.serialization, and I'd like to define these classes ideally in the projet's buildSrc/.
I haven't found any resources online for trying to setup serialization within gradle's build process, and not just configuring it for the app at runtime. This is what I've put together as my buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
kotlin("plugin.serialization") version "1.3.72"
}
repositories {
jcenter()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0")
}
And this is my test data class:
#Serializable
data class JsonGenerator(val x: String = "")
The error I get is:
Cannot access 'Serializable': it is internal in 'kotlin.io'
It seems that this error can happen when the dependency isn't properly declared. But I'm still unclear whether buildSrc has restrictions that make this impossible or not. I'm not married to this approach, but this seemed like the best solution.
Edit:
I've changed my buildSrc/build.gradle.kts to:
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
dependencies {
implementation(kotlin("serialization"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")
}
Structure:
buildSrc
main
Kotlin
MyTempClass.kt
It is important for the kt file to not be nested, I think that will only work if the file is the same name as the nesting directory
Inside of my data class file:
import kotlinx.serialization.*
#Serializable
data class MyTempClass(val name: String)
I had a specific implementation of serialization, without the plugin.
Then in the top level build.gradle.kts, the MyTempClass was accessible

Getting org.junit.runner.RunWith is not an annotation type with Cucumber

I'm trying to set-up a Selenium + Cucumber project in Java (I'm a beginner with Java) in Intellij. This is my class that uses the Cucumber JUnit runner:
package runtest;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions (
features = "C:\\Users\\my-feature.feature",
)
public class RunTest {
}
I'm getting these errors:
Error:(8, 1) java: annotation #org.junit.runner.RunWith is missing default values for elements annotationType,<init>
Error:(8, 2) java: org.junit.runner.RunWith is not an annotation type
I don't know how to resolve this or what is happening. Please help.
I had multiple RunWith classes in my project path. After pruning the project tree, it works as expected.
You don't have to provide the absolute path of the feature file if you opt to place it within your project space. The name of the feature file folder would be enough.
As an example, if you are having the following structure:
Package: Q43966680
Class: Runner
Feature File Folder: q43966680
Snapshot:
As per your usecase within #CucumberOptions you just need to mention:
#RunWith(Cucumber.class)
#Cucumber.Options (features="q43966680")
public class RunTest {
}

gradle test raise error: java.lang.NoClassDefFoundError: Could not initialize class groovy.lang.GroovySystem

I try to run "gradle test", and get error
My test is
class HelperTest extends ro.gd.Test {
Plugin o;
void setUp() {
o = new Plugin();
}
void testGetIdeaDeps() {
def r = o.ideaDeps
asrHaveVal r
}
}
when i run gradle test, it raise:
junit.framework.AssertionFailedError: Exception in constructor: testGetIdeaDeps (java.lang.NoClassDefFoundError: Could not initialize class groovy.lang.GroovySystem
at org.codehaus.groovy.reflection.ClassInfo.isValidWeakMetaClass(ClassInfo.java:221)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassForClass(ClassInfo.java:191)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:236)
at ro.gd.idea.HelperTest.$getStaticMetaClass(HelperTest.groovy)
at ro.Test.<init>(Test.groovy)
at ro.gd.Test.<init>(Test.groovy)
at ro.gd.idea.HelperTest.<init>(HelperTest.groovy)
...
Here is my full code
I fix this question. the reason is "groovy.lang.GroovyRuntimeException: Conflicting module versions", for detail, following is my build.gradle
compile 'org.codehaus.groovy:groovy-all:+'
compile gradleApi()
I guess gradleApi() will auto "compile localGroovy()", and this groovy version is 2.3.6, but latest version is 2.4.3
I find this error message in one test report
the solution is to specify groovy version like following
compile 'org.codehaus.groovy:groovy-all:2.3.6'
and you check your groovy version with 'gradle dependencies|grep groovy'