Testing against Kotlin at LightCodeInsightFixtureTestCase

Answered

Need to test highlighting at Kotlin files:

public class KdcrCodeInsightTest extends LightCodeInsightFixtureTestCase {

public void testHighlighting() {
doKotlinTest("fun foo(){ ");
doJavaTest("class MyClass { ");
}

private long doKotlinTest(@NotNull String testingText) {
myFixture.configureByText("myTmpFile.kt", testingText);
return myFixture.checkHighlighting(false, true, false, false);
}

private long doJavaTest(@NotNull String testingText) {
myFixture.configureByText("myTmpFile.java", testingText);
return myFixture.checkHighlighting(false, true, false, false);
}
}

doKotlinTest() passed and doJavaTest() fails (with <error descr="'}' expected"> </error> as expected).

Looks like testing environment doesn't have Kotlin support and treat any `.kt` file as just plain text.

Trying to add `kotlin-plugin.jar` as a library through overriding getProjectDescriptor() doesn't help either:

@Override
@NotNull
protected LightProjectDescriptor getProjectDescriptor() {
return new DefaultLightProjectDescriptor(){
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
super.configureModule(module, model, contentEntry);
PsiTestUtil.addLibrary(module, getBundledKotlinJarPath());
}

private String getBundledKotlinJarPath() {
String jarPath = PathManager.getJarPathForClass(Kotlin.class);
assert jarPath != null : "Cannot find JAR containing Kotlin classes";
return jarPath;
}
};
}

When looking around through other plugins source: its seems that Kotlin support works out of the box for them or adding the syntax-supporting plugin as a library works (like for tests in Groovy plugin).

I feel like missing some stupidly-simple setup step... Any bits of advice are highly appreciating.

6 comments
Comment actions Permalink

You must have a dependency on Kotlin plugin in the classpath of executing your tests. ProjectDescriptor describes test project itself, not the test environment IDE.

1
Comment actions Permalink

I am also running into the same issue with checkHighlighting tests for kotlin files. I'm still not clear on how to set this up though. I already have the dependency on kotlin plugin in my plugin.xml file.

<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.kotlin</depends>


How do I set this up for my fixture that LightCodeInsightTestFixtureTestCase? I already have the MockJdk set up in the project descriptor here:

override fun getSdk(): Sdk {
return IdeaTestUtil.getMockJdk17()
}
0
Comment actions Permalink

Ksrinivasan So the test environment doesn't treat .kt files as Kotlin source? This probably means that Kotlin plugin is not loaded at all in your tests. Please link to your repository if available, this will make it much easier to understand your current setup. Thanks.

0
Comment actions Permalink

Thanks for the response! Yes, that's correct. The kotlin plugin is not loaded for tests, but not sure what is missing in the set up. Here is the repo for reference:
https://github.com/Netflix/dgs-intellij-plugin/tree/main/src/test/kotlin/com/netflix/dgs/plugin

0
Comment actions Permalink

Please always update to latest version of Gradle IntelliJ plugin: id("org.jetbrains.intellij") version "1.2.1" -> 1.3.1

Why do you build against IU-213-EAP-SNAPSHOT (https://github.com/Netflix/dgs-intellij-plugin/blob/main/gradle.properties#L34)? We recommend using minimum supported version (usually stable one), so 211. Also review pluginVerifierIdeVersions property accordingly.

0
Comment actions Permalink

Thanks, I will try that.

0

Please sign in to leave a comment.