In my Gradle project, spring cloud contract tests always fail once after clean.
./gradlew clean
./gradlew latte:test (fails ❌)
./gradlew latte:test (passes ✅)
example project: https://github.com/lnhrdt/coffee.git
use commit: 14fe147013
.../coffee/latte/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/friends/RestTest.java:7: error: package io.leonhardt.coffee.latte.contracts does not exist
import io.leonhardt.coffee.latte.contracts.FriendsRestBase;
^
.../coffee/latte/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/friends/RestTest.java:14: error: cannot find symbol
public class RestTest extends FriendsRestBase {
^
symbol: class FriendsRestBase
Any ideas?
That's superstrange... I'll try to check it out this week. You could check out if anything gets generated when you run it for the second time. Also can you show the whole flow of tasks?
Also can you show the whole flow of tasks?
sure thing:
./gradlew clean latte:test
:latte:clean
:latte:compileKotlin
Using kotlin incremental compilation
:latte:compileJava NO-SOURCE
:latte:compileGroovy NO-SOURCE
:latte:copyMainKotlinClasses
:latte:processResources
:latte:classes
:latte:compileTestKotlin
Using kotlin incremental compilation
:latte:copyContracts
:latte:generateContractTests
:latte:compileTestJava NO-SOURCE
:latte:compileTestGroovy
.../coffee/latte/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/friends/RestTest.java:7: error: package io.leonhardt.coffee.latte.contracts does not exist
import io.leonhardt.coffee.latte.contracts.FriendsRestBase;
^
.../coffee/latte/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/friends/RestTest.java:14: error: cannot find symbol
public class RestTest extends FriendsRestBase {
^
symbol: class FriendsRestBase
2 errors
startup failed:
Compilation failed; see the compiler error output for details.
1 error
:latte:compileTestGroovy FAILED
:latte:copyTestKotlinClasses
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':latte:compileTestGroovy'.
> Compilation failed; see the compiler error output for details.
* 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: 4.581 secs
Could you run this twice (the negative and then the positive scenario) with --debug flag and attach the output?
Aha! On this line the generateContractTests task is configured to depend on compileTestJava but I'm writing in Kotlin, which has the task compileTestKotlin. Perhaps we should change it to depend on the higher-level testClasses.
ha! Good point!
Care for a PR? :)
Already working on it! Trying to figure out how to build the gradle plugin locally and use it in my project to validate the change. If you have any tips let me know. Pausing now, I'm going to dig in again around 1700 CDT.
Sure... go to the plugin folder and type ./gradlew clean build install. That way you'll install the latest snapshots in your .m2
After digging in it seems this is more complex than I understood before. I don't have a solution but figured I'd document my context in case I don't come back to this and someone else wants to pick it up or if I need to remind myself what's going on when I can pick this up again.
TL;DR
it's a present incompatibility betwen the kotlin-gradle-plugin and our spring-cloud-contract-gradle-plugin, when the base contract classes are written in Kotlin.
While Java and Groovy have the tasks compileJava, compileGroovy, compileTestJava, and compileTestGroovy, Kotlin has the tasks compileKotlin, copyMainKotlinClasses, compileTestKotlin, and copyTestKotlinClasses. These extra copy* tasks are attached to the lifecycle by finalizing some related Java task (source code). In the scenario with our spring-cloud-contract-gradle-plugin and my Kotlin project, compileTestGroovy needs the copyTestKotlinClasses to have run, or else the compiled *RestBase classes won't exist. As it stands now, compileTestGroovy always runs after compileTestGroovy so it fails on the first run after a clean.
So for sure the workaround for you is to write
compileTestGroovy.dependsOn 'copyTestKotlinClasses'
I'll try to programmatically make this work from inside the plugin
I gave this a thought and I don't think I should be doing that in the plugin. You can set the tests to be JUnit tests and then you don't have to the workaround. Until then I guess we should wait for more people to express their opinion on this. I'm kind of afraid that changing the order in every project can lead to some issues...
So for now I'm closing this one. We have a workaround to which we can stick at the moment.