Errors when doing "gradle test" locally

I successfully installed gradle , and downloaded NeedForSpeed exercise for Java. I created my solution for this exercise inside two files, NeedForSpeed.java and RaceTrack.java, since solution requires 2 classes.

I’m using Java 20, and Gradle 8.3. JAVA_HOME environment variable is configured, and both java and gradle are included in the PATH variable as well.

I’m using IntelliJ IDEA, and inside Settings → Build, Execution, Deployment → Build Tools → Gradle, I’ve set my Gradle distribution to local installation, with path being “C:/Program Files/Gradle/gradle-8.3” where I have it installed. Gradle JVM is set to JAVA_HOME, i.e. my Java 20 JDK installation.

When I do gradle test, I get this error

Task :compileTestJava FAILED
C:\Users\MyUserName\Exercism\java\need-for-speed\src\test\java\NeedForSpeedTest.java:122: error: cannot find symbol
var race = new RaceTrack(distance);
^
symbol: class RaceTrack
location: class NeedForSpeedTest
C:\Users\MyUserName\Exercism\java\need-for-speed\src\test\java\NeedForSpeedTest.java:134: error: cannot find symbol
var race = new RaceTrack(distance);
^
symbol: class RaceTrack
location: class NeedForSpeedTest
C:\Users\MyUserName\Exercism\java\need-for-speed\src\test\java\NeedForSpeedTest.java:146: error: cannot find symbol
var race = new RaceTrack(distance);
^
symbol: class RaceTrack
location: class NeedForSpeedTest
C:\Users\MyUserName\Exercism\java\need-for-speed\src\test\java\NeedForSpeedTest.java:158: error: cannot find symbol
var race = new RaceTrack(distance);
^
symbol: class RaceTrack
location: class NeedForSpeedTest
4 errors

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:compileTestJava’.
    Compilation failed; see the compiler error output for details.

  • Try:
    Run with --info option to get more log output.
    Run with --scan to get full insights.

BUILD FAILED in 872ms
2 actionable tasks: 1 executed, 1 up-to-date
PS C:\Users\MyUserName\Exercism\java\need-for-speed>

Hmm … I wonder if gradle isn’t recompiling the classes after splitting the classes into separate files… Did you happen to run gradle test before moving RaceTrack into RaceTrack.java? I suggest try running a gradle clean and then gradle test.

If you’re wondering, I managed to reproduce your error with the following steps:

  1. Download the exercise
  2. Run gradle test without making any changes (I get tests failing but not any of the cannot find symbol errors)
  3. Make RaceTrack.java and move the RaceTrack class there
  4. Run gradle test again. At this stage, I get the cannot find symbol errors.

Running gradle clean and then gradle test fixed it for me.