How to fix ‘compileTestJava error: cannot find symbol’ java and groovy test folder

Hi everyone,
I’m trying to execute the gradle command gradle clean build and I’m gettin the following error:

> Task :compileTestJava
/home/jppazmin/fire08Dec/fire-api/src/test/java/com/opessoftware/fire/api/test/TestAccountCalculationWrongData.java:20: error: package com.opessoftware.fire.api.test.groovy.helper does not exist
import com.opessoftware.fire.api.test.groovy.helper.TestGroovyHelper;
                                                   ^
/home/jppazmin/fire08Dec/fire-api/src/test/java/com/opessoftware/fire/api/test/TestAccountCalculationWrongData.java:31: error: cannot find symbol
        private TestGroovyHelper testGroovyHelper;
                ^
  symbol:   class TestGroovyHelper
  location: class com.opessoftware.fire.api.test.TestAccountCalculationWrongData
2 errors

> Task :compileTestJava FAILED

FAILURE: Build failed with an exception.

As you can see I have TestAccountCalculationWrongData.java in src/test/java folder this class has a reference to TestGroovyHelper.java which is in src/test/groovy folder, the strange thing of this error is I execute the test from Eclipse IDE and it works well, but as soon as I try execute in console gradle clean build or gradle clean test I get the compilation error.

Here my build.gradle configuration:

plugins {
	id 'org.springframework.boot' version '2.6.6'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
	id 'org.ajoberstar.grgit' version '4.1.1'
	id 'groovy'
}

group = 'com.opessoftware'
version = '15.1.9-RELEASE' + getVersionNameExtension()

java {
	toolchain {
		String useNewJavaVersion = System.getProperty("app.java.version", "8")
		int javaVersion = useNewJavaVersion.toInteger() != 8 ? useNewJavaVersion.toInteger() : 8
		languageVersion = JavaLanguageVersion.of(javaVersion)
	}
}

bootJar {
	archiveFileName = "${archiveBaseName.get()}.${archiveExtension.get()}"
}

repositories {
	mavenLocal()
	mavenCentral()
	gradlePluginPortal()
	maven {
		url "${nexusUrlBase}:${nexusGradlePort}/repository/fire-releases"
		credentials {
			username System.getenv('NEXUS_USERNAME')
			password System.getenv('NEXUS_PASSWORD')
		}
	}
}


ext {
	set('springCloudVersion', '2021.0.1')

	set('apacheCommonsDbcp2Version', '2.9.0')
	set('apachePoiVersion', '5.0.0')
	set('awaitilityVersion', '3.0.0')
	set('commonsIoVersion', '2.11.0')

	set('opesAccountsClientVersion', '3.0.0-RELEASE')
	set('opesAuditClientVersion', '3.0.0-RELEASE')
	set('opesAuthClientVersion', '3.0.0-RELEASE')
	set('opesBackendLibraryVersion', '3.0.0-RELEASE')
	set('opesCommonBeansVersion', '3.0.0-RELEASE')
	set('opesCqrsClientVersion', '3.0.0-RELEASE')
	set('opesCrsClientVersion', '3.0.0-RELEASE')
	set('opesCrsProcessorVersion', '3.0.0-RELEASE')
	set('opesCustomersClientVersion', '3.0.0-RELEASE')
	set('opesFatcaClientVersion', '3.0.0-RELEASE')
	set('opesFatcaProcessorVersion', '3.0.0-RELEASE')
	set('opesInstitutionsClientVersion', '3.0.0-RELEASE')
	set('opesFilestorageClientVersion', '3.0.0-RELEASE')
	set('opesFireBeansVersion', '3.0.0-RELEASE')
	set('opesFireLibraryVersion', '3.0.0-RELEASE')
	set('opesFireviewClientVersion', '3.0.0-RELEASE')
	set('opesKycClientVersion', '3.0.0-RELEASE')
	set('opesMakerCheckerClientVersion', '3.0.0-RELEASE')
	set('opesMqLibraryVersion', '3.0.0-RELEASE')
	set('opesNotificationsClientVersion', '3.0.0-RELEASE')
	set('opesUsersClientVersion', '3.0.0-RELEASE')

	set('junitVersion', '4.13.2')
	set('spockVersion', '2.0-groovy-3.0')
	set('groovyVersion', '3.0.9')
 	set('xmlUnitMatchersVersion', '2.8.2')
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	implementation 'org.springframework.boot:spring-boot-starter-actuator'
	implementation 'org.springframework.boot:spring-boot-starter-websocket'
	implementation 'org.springframework:spring-aspects'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
	implementation 'org.springframework.boot:spring-boot-starter-validation'

	implementation "org.apache.commons:commons-dbcp2:${apacheCommonsDbcp2Version}"
	implementation "org.apache.poi:poi-ooxml:${apachePoiVersion}"
	implementation "org.awaitility:awaitility:${awaitilityVersion}"
	implementation "commons-io:commons-io:${commonsIoVersion}"

	implementation "com.opessoftware.library:accounts-client:${opesAccountsClientVersion}"
	implementation "com.opessoftware.library:audit-client:${opesAuditClientVersion}"
	implementation "com.opessoftware.library:auth-client:${opesAuthClientVersion}"
	implementation "com.opessoftware.library:backend-library:${opesBackendLibraryVersion}"
	implementation "com.opessoftware.library:common-beans:${opesCommonBeansVersion}"
	implementation "com.opessoftware.library:crs-client:${opesCrsClientVersion}"
	implementation "com.opessoftware.library:crs-processor:${opesCrsProcessorVersion}"
	implementation "com.opessoftware.library:cqrs-client:${opesCqrsClientVersion}"
	implementation "com.opessoftware.library:customers-client:${opesCustomersClientVersion}"
	implementation "com.opessoftware.library:fatca-client:${opesFatcaClientVersion}"
	implementation ("com.opessoftware.library:fatca-processor:${opesFatcaProcessorVersion}") {
		exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
	}
	implementation "com.opessoftware.library:filestorage-client:${opesFilestorageClientVersion}"
	implementation "com.opessoftware.library:fire-beans:${opesFireBeansVersion}"
	implementation "com.opessoftware.library:fire-library:${opesFireLibraryVersion}"
	implementation "com.opessoftware.library:fireview-client:${opesFireviewClientVersion}"
	implementation "com.opessoftware.library:institutions-client:${opesInstitutionsClientVersion}"
	implementation "com.opessoftware.library:kyc-client:${opesKycClientVersion}"
	implementation "com.opessoftware.library:makerchecker-client:${opesMakerCheckerClientVersion}"
	implementation "com.opessoftware.library:mq-library:${opesMqLibraryVersion}"
	implementation "com.opessoftware.library:notifications-client:${opesNotificationsClientVersion}"
	implementation "com.opessoftware.library:users-client:${opesUsersClientVersion}"

	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.security:spring-security-test'

	testImplementation "junit:junit:${junitVersion}"
	testImplementation "org.xmlunit:xmlunit-matchers:${xmlUnitMatchersVersion}"

	testImplementation "org.spockframework:spock-core:${spockVersion}"
	testImplementation "org.spockframework:spock-spring:${spockVersion}"
	testImplementation "org.codehaus.groovy:groovy-all:${groovyVersion}"
}

test {
	useJUnitPlatform()
}

static getVersionNameExtension() {
	try {
		def git = org.ajoberstar.grgit.Grgit.open()
		def versionNameSuffix = "-${git.head().getAbbreviatedId(8)}"
		git.close()
		return versionNameSuffix
	} catch (ignored) {

		return ""
	}
}

processResources {
	inputs.file "build.gradle"
	filesMatching("**/*.yml") {
		expand project.properties
	}
}

bootBuildImage {
	imageName = "${nexusDomainHost}:${nexusDockerPort}/repository/docker-private/${project.name}:${project.version}"
	publish = false
	docker {
		publishRegistry {
			username = System.getenv('NEXUS_USERNAME')
			password = System.getenv('NEXUS_PASSWORD')
		}
	}
}

I don’t know if I missing something in the build.gradle configuration.
I would be very grateful for any help for this issue.
Thanks in advance!

By default Groovy classes can use Java classes from the same source set, but not the other way around.
If you instead want the Java compilation task have the Groovy classes available, you need to change the dependency order as documented at Building Java & JVM projects.

Another option would be to use a separate source set like with the java-test-fixtures plugin: Testing in Java & JVM projects, or a separate project that you depend on, where you have the “common” classes.

Yet another option specifically for Groovy is joint compilation. The Groovy compile is able to do joint compilation where Java and Groovy classes can be freely mixed dependency-wise. For that just put the relevant java files into src/main/groovy intead of src/main/java or change the configuration of the Groovy compilation task to also look at src/main/java.

And one more option, don’t use two languages for your tests, but stick with one. Actually, I’d recommend to use Spock for test and thus your tests are written in Groovy anyway, but I’m biased. :smiley: