Configuring VSCode for Mockito unit testing

I’ve managed to create some unit tests using Mockito on my team’s 2021 project for the drive subsystem. I’ve gotten the test to successfully run, however, VSCode doesn’t provide any autocompletion or javadoc tooltips in the test file. It also doesn’t recognise the tests and bring up the “run test” and “debug test” buttons on the file. And it reports the following:
DriveSubsystemTest.java is not on the classpath of project TB2021, only syntax errors are reported

DriveSubsystemTest.java is located in TB2021/src/test/java/frc/robot/subsystems/DriveSubsystemTest.java.

I’ve tried adding another source set in the build.gradle file with the following addition:

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
    }
    test {
        java {
            srcDir 'src/test/java'
        }
    }
}

Have you tried using IntelliJ? I’ve always found vscode’s java code completion to be brittle and frustrating.

Good choice of mocking framework, by the way.

I haven’t tried using IntelliJ, and I’m not really considering it at the moment for this. VSCode has had pretty good code completion for me in the rest of the robot code, and outside of the WPILib extensions, is problem free. Only WPILib crap is buggy as hell…

For Java, all the intellisence including for testing is handled by the redhat Java extension and the Java test extension.

Usually when the test classpath can’t be counted is because testing wasn’t enabled at the start, and the Java extension doesn’t refresh correctly. You can have it completely refresh by running the ‘Clean Java Workspace’ command from the command pallet. After resetting at least intellisense should work.

3 Likes

For the run tests buttons to work, you have to do the following.

As long as I remember, the 2022 templates will have this part by default.

1 Like

Thanks, that worked.

Interestingly, if I disable the ‘Test Runner for Java’ extension and reload the ‘Language Support for Java’ extension, Intellisense starts working again. But with the ‘Test Runner’ extension enabled, Intellisense doesn’t work. Not sure if anyone else has seen this issue.

Thats bizarre for sure. It doesn’t 100% surprise me, as those extensions are written by separate companies and its possible something broke in one of the 2 extensions.

I can dig through the issues later to check. You should be able to reproduce this 100% with just a normal Java project, as at least for Java we don’t interface with the intellisense engine at all.

You also might be able to run on logging for both extensions (They both have a way to do it I think) and that might give some insight.

1 Like

Figured out a solution for now. I’ve downgraded the Red Hat Java extension to version 0.81.0. That fixes the issue. I can now test and enjoy IntelliSense at the same time. Red Hat must have broken something in the later versions.

Are you using the beta build? Have you posted your observations on the beta forum as far as the extension version?

Must have just been some weirdness on my end. I upgraded back to the latest version of the Red Hat Java extension and its working fine. IntelliSense and testing both work.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.