Gradle integration test in Kotlin with Java project - java

I have a Gradle project written in Java. I want to set up integration tests in Kotlin.
For that i used JVM Test Suite Plugin as described here. Gradle version is 7.5.1
When i try to build the project, it fails with:
BUILD FAILED in 2s
14 actionable tasks: 1 executed, 13 up-to-date
slawomir.filip#polpc10555 myproject_backend % ./gradlew integrationTest
> Task :compileIntegrationTestKotlin FAILED
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/BaseIT.kt: (3, 41): Unresolved reference: repository
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/BaseIT.kt: (19, 15): Only 'const val' can be used in constant expressions
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/BaseIT.kt: (19, 16): Unresolved reference: BackendApplication
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/BaseIT.kt: (19, 16): An annotation argument must be a compile-time constant
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/BaseIT.kt: (32, 40): Unresolved reference: OptionsJpaRepository
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (6, 41): Unresolved reference: dto
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (7, 41): Unresolved reference: repository
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (16, 35): Unresolved reference: OptionsEntity
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (17, 35): Unresolved reference: OptionsEntity
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (18, 35): Unresolved reference: OptionsEntity
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (22, 24): Not enough information to infer type variable T
e: /Users/slawomir.filip/Documents/custom/myproject_backend/src/integrationTest/kotlin/com/myproject/backend/domainoptions/GeneralInfoControllerSpec.kt: (22, 58): Unresolved reference: GeneralInfoDto
FAILURE: Build failed with an exception.
for this example test:
package com.myproject.backend.domainoptions
import org.amshove.kluent.shouldBeEqualTo
import com.myproject.backend.BaseIT
import com.myproject.backend.domainoptions.dto.GeneralInfoDto
import com.myproject.backend.domainoptions.repository.entity.OptionsEntity
import org.junit.jupiter.api.Test
import org.springframework.http.HttpStatus
class GeneralInfoControllerSpec : BaseIT() {
#Test
fun `should test something`() {
optionsJpaRepository.save(OptionsEntity("sth1", "14"))
optionsJpaRepository.save(OptionsEntity("sth2", "11"))
optionsJpaRepository.save(OptionsEntity("sth3", "T"))
val response = makeGetRequest("/options", GeneralInfoDto::class.java)
response.statusCode `shouldBeEqualTo` HttpStatus.OK
}
}
Gradle doesn't see project internal dependencies (as for example: OptionsEntity) and I don't know why.
Structure of my project is:
- src
- integrationTest
- kotlin
- resources
- main
- java
- resources
- test
- java
- resources
Here is my build.gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'io.freefair.lombok' version '5.3.3.3'
id 'org.jetbrains.kotlin.jvm' version '1.6.0'
}
group = 'com.myproject'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
apply plugin: 'application'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies { /* my dependencies */ }
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
compileKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
integrationTestImplementation.extendsFrom testImplementation
integrationTestImplementation.extendsFrom implementation
}
test {
useJUnitPlatform()
}
testing {
suites {
integrationTest(JvmTestSuite) {
dependencies {
implementation project
}
}
}
}
tasks.named('check') {
dependsOn testing.suites.integrationTest
}
apply plugin: 'war'
apply plugin: 'kotlin'
Is it even possible to do this? To have kotlin tests for java project?
If yes, what do I miss here?

I added
sources {
java {
srcDirs = ['src/main/java', 'src/integration/kotlin', 'src/integration/resources']
}
}
so the result is
testing {
suites {
integrationTest(JvmTestSuite) {
dependencies {
implementation project
annotationProcessor 'org.projectlombok:lombok'
}
sources {
java {
srcDirs = ['src/main/java', 'src/integration/kotlin', 'src/integration/resources']
}
}
}
}
}
I don't know if this is the good approach, but it works

Related

Web3j plugin for gradle task not found

I am beating against a wall for a whole week. I can't really see the reason why it doesn't work. I have a project where I want to run my Java project with the following build.gradle config:
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.3'
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id 'org.web3j' version "4.8.4"
}
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'com.suplab'
sourceSets {
main {
solidity {
srcDir "contracts"
}
}
test {
solidity {
srcDir "contracts"
}
}
}
ext {
web3jVersion = '4.8.4'
}
npmInstall {
enabled false
}
dependencies {
implementation "org.web3j:core:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-core:5.6.5.Final'
}
I use gradle version 6.8, since it was specified by developer of the plugin. When I try to build this project from the parent, root folder that is the root of the project, it fails with following error:
A problem occurred configuring project ':api'.
> Failed to notify project evaluation listener.
> Task with name 'resolveSolidity' not found in project ':api'.
> Could not create task ':api:generateTestContractWrappers'.
> Task with name 'compileTestSolidity' not found in project ':api'.
I can't figure it out. I checked it against the build that being generated with web3j-cli, and it's the same for the configuration property (both configs have it blank). So, I don't see any reason WHY it fails. Here's the webj3-cli generated build file:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
id "com.github.johnrengelman.shadow" version "5.2.0"
id 'org.web3j' version '4.8.4'
}
group 'org.web3j'
version '0.1.0'
sourceCompatibility = 1.8
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
web3j {
generatedPackageName = 'org.web3j.generated.contracts'
excludedContracts = ['Mortal']
}
ext {
web3jVersion = '4.8.4'
logbackVersion = '1.2.3'
}
dependencies {
implementation "org.web3j:core:$web3jVersion",
"ch.qos.logback:logback-core:$logbackVersion",
"ch.qos.logback:logback-classic:$logbackVersion"
implementation "org.web3j:web3j-unit:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
jar {
manifest {
attributes(
'Main-Class': 'org.web3j.Web3App',
'Multi-Release':'true'
)
}
}
application {
mainClassName = 'org.web3j.Web3App'
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

Why does the Java compiler not find dependency modules in Gradle subprojects?

Exactly what the title says. For the record, I am using OpenJDK 15.0.1 and Gradle 6.7. I am trying to run code in a subproject with dependencies. When I try, I get a compiler error like so:
> Task :browser-core:compileJava FAILED
1 actionable task: 1 executed
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:3: error: module not found: javafx.graphics
requires javafx.graphics;
^
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:5: error: module not found: net.sf.cssbox
requires net.sf.cssbox;
^
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:6: error: module not found: net.sf.cssbox.jstyleparser
requires net.sf.cssbox.jstyleparser;
^
As far as I'm concerned, the dependencies in question (JavaFX, CSSBox) are already specified under the root build.gradle file. Why does the compiler overlook these dependencies, and how do I fix it?
For reference, here's my root build.gradle:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
java {
modularity.inferModulePath = true
}
allprojects {
plugins.withType(JavaPlugin, {
dependencies {
//determine dependency names
def os = org.gradle.internal.os.OperatingSystem.current()
def fxDep = ""
if (os.isWindows()) {
fxDep = "win"
}
else if (os.isMacOsX()) {
fxDep = "mac"
}
else if (os.isLinux()) {
fxDep = "linux"
}
//implementation 'org.slf4j:slf4j-jdk14:1.7.30'
implementation 'org.slf4j:slf4j-nop:1.7.30'
implementation('net.sf.cssbox:cssbox:5.0.0') {
exclude group: 'xml-apis', module: 'xml-apis'
}
implementation "org.openjfx:javafx-base:15.0.1:${fxDep}"
implementation "org.openjfx:javafx-controls:15.0.1:${fxDep}"
implementation "org.openjfx:javafx-graphics:15.0.1:${fxDep}"
implementation "org.openjfx:javafx-fxml:15.0.1:${fxDep}"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
}
})
}
test {
useJUnitPlatform()
}
and the subproject's build.gradle:
plugins {
id 'application'
}
group 'io.github.jgcodes'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
It turns out that
java {
modularity.inferModulePath = true;
}
needs to be set for all subprojects individually. Specifically, you need to place it in the allprojects closure.

No signature of method is applicable for argument types when trying to publish to JCenter with gradle

I'm trying to publish my project to JCenter with gradle.
I'm getting this error:
$ ./gradlew bintrayUpload
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/tomcaflisch/my-project/build.gradle' line: 32
* What went wrong:
A problem occurred evaluating root project 'my-project'.
> No signature of method: build_1izvkzzfcfkppgubyut54udzf.bintray() is applicable for argument types: (build_1izvkzzfcfkppgubyut54udzf$_run_closure3) values: [build_1izvkzzfcfkppgubyut54udzf$_run_closure3#30577a07]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
Here's my build.gradle file
buildscript {
repositories {
maven { url "https://repo1.maven.org/maven2" }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.+'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
plugins {
id "com.jfrog.bintray" version "1.8.5"
}
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'java'
group = 'com.projectx'
version = '0.0.1'
repositories {
jcenter()
mavenCentral()
}
sourceSets {
main.java.srcDirs = ['src/main/java']
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publications = ['MyPublication']
pkg {
repo = 'maven'
name = 'project-x'
userOrg = 'myorg'
licenses = ['Proprietary']
vcsUrl = 'https://github.com/myorg/my-project.git'
version {
name = project.version
released new Date()
}
}
}
publishing {
publications {
MyPublication(MavenPublication) {
from components.java
groupId project.group
artifactId 'projectx'
version project.version
}
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'jacoco'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
install {
repositories.mavenInstaller {
pom.artifactId = 'projectx'
}
}
test {
finalizedBy jacocoTestReport // Report is always generated after tests run
}
jacocoTestReport {
dependsOn test // Tests are required to run before generating the report
reports {
xml.enabled true
}
}
task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
}
dependencies {
compile 'io.swagger:swagger-annotations:1.5.22'
compile "com.google.code.findbugs:jsr305:3.0.2"
compile 'com.squareup.okhttp3:okhttp:3.14.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.14.2'
compile 'com.google.code.gson:gson:2.8.5'
compile 'io.gsonfire:gson-fire:1.8.3'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
testCompile 'junit:junit:4.13'
testCompile 'org.mockito:mockito-core:3.+'
testImplementation "com.google.truth:truth:1.0.1"
}
javadoc {
options.tags = [ "http.response.details:a:Http Response Details" ]
}
Bintray is dead.
But to answer the question, No signature of method: build_whatever.[method]() comes from something in the configuration of [method] being incorrect. In this case I'd guess it's caused by released new Date() which should probably be released = new Date()

gradle generates protobuf class but shows compile error

The generated protobuf class is under generated-sources as expected.
But it has references to com.google.protobuf, for example below code. And I get compilation error saying com.google.protobuf not found.
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
The below is my build.gradle file.
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
I think the problem is that the protobuf library is not showing up in the external libraries of my intellij project. Is there a way to make it work with gradle?
Working gradle file:
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.protobuf:protobuf-java:3.11.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.6.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}

Mixing Java and Kotlin with gradle - NoClassDefFoundError

I am trying to compile a gradle projet where I have mixed kotlin and java files under src/main/java and src/main/kotlin.
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version "1.3.41"
}
ext {
kotlinVersion = '1.3.41'
}
group 'github.littlechisels'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
sourceSets {
main.java.srcDirs = ['src/main/java']
main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
main.resources.srcDirs = []
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
...
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
testImplementation("org.junit.jupiter:junit-jupiter:5.5.0")
testRuntime ('org.junit.jupiter:junit-jupiter-engine:5.5.0')
testImplementation ('org.junit.jupiter:junit-jupiter-api:5.5.0')
test.useJUnitPlatform()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
but build\classes\java\main is empty while kotlin one is not
Project builds fine with both IntelliJ and Gradle, but when I run littlechisels.main.Converter I get a NoClassDefFoundError about java classes :
Exception in thread "main" java.lang.NoClassDefFoundError: littlechisels/main/WorldSave
at littlechisels.main.Converter.main(Converter.kt:20)
Caused by: java.lang.ClassNotFoundException: littlechisels.main.WorldSave
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)
... 1 more
Repo can be found here https://github.com/vinz243/littlechisels

Categories

Resources