How do I create unit tests?

The docs are not sufficient for me to create any working unit tests, every class referenced in the docs doesn’t exist and the dependency I need to pull isn’t mentioned anywhere!

I’m trying to run a test with Psi and when I try and resolve java.lang.String I get class not found exception…

What am I missing?

I see in the docs you need something like

test {
systemProperty(“idea.home.path”, “/path/to/intellij-community-sources”)
}

but I’m unsure what intellij-community-sources it supposed to mean? I see on github people use stuff like

systemProperty(“idea.home.path”, idea.ideaHome)
158

or

systemProperty(“idea.home.path”, intellijRootDir().canonicalPath)
176

But I can’t reference these.

Did you look at GitHub - JetBrains/intellij-platform-plugin-template: Template repository for creating plugins for IntelliJ Platform?

Yep, tests are painful.

I’ve spent several days to reach a working solution, here what I have:

  • libs.versions.toml
junit = "4.13.2"
  • build.gradle.kts

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")
    testImplementation(libs.junit)
    testImplementation("org.junit.jupiter:junit-jupiter:5.12.2")
    testImplementation(kotlin("test"))
    // ...

    intellijPlatform {
        // ...
        testFramework(TestFrameworkType.Platform)
        testFramework(TestFrameworkType.JUnit5)
    }
}
// ...
tasks {
    // ...
    test {
        jvmArgumentProviders += CommandLineArgumentProvider {
            listOf(
                "-Didea.home.path=build/idea-sandbox/${prop("platformType")}-${prop("platformVersion")}",
            )
        }
        systemProperty("path.to.build.plugin", buildPlugin.get().archiveFile.get().asFile.absolutePath)
        useJUnitPlatform()
    }
}
// ...

intellijPlatformTesting {
    runIde {
        register("runIdeForUiTests") {
            task {
                jvmArgumentProviders += CommandLineArgumentProvider {
                    listOf(
                        "-Drobot-server.port=8082",
                        "-Dide.mac.message.dialogs.as.sheets=false",
                        "-Djb.privacy.policy.text=<!--999.999-->",
                        "-Djb.consents.confirmation.enabled=false",
                    )
                }
            }

            plugins {
                robotServerPlugin()
            }
        }
    }
}

And now you’re ready to starting writing Unit+Functional tests.

This is example of my own:


import com.intellij.testFramework.fixtures.BasePlatformTestCase

class AppendTransformerTest : BasePlatformTestCase() {
    fun testProcessText() {
        val transformer = AppendTransformer("value/to/append")

        val result = transformer.process("new value", myFixture.project)
        val expected = "value/to/append/new value"

        assertEquals(expected, result)
    }
}

If you’re looking for JUnit annotations, I you those from jupiter:


import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
abstract class BaseFeatureTest : BasePlatformTestCase() {
    abstract fun dataDefaultCompletion(): Collection<Arguments>

    @ParameterizedTest
    @MethodSource("dataDefaultCompletion")
    open fun testCompletion(definitions: String, code: String, completion: Array<String>) {
        addXmlDefinition(definitions)
        loadPhpContent(code)

        assertCompletionContains(*completion)
    }

    @BeforeEach
    override fun setUp() {
        super.setUp()
    }

    @AfterEach
    override fun tearDown() {
        super.tearDown()
    }
}

Hope it helps

No - that looks helpful, I’ll start a new project with this.

I have migrated over to the template, and I still cannot test out any java code.

Cannot resolve class: java.lang.String

trying to call PsiClassType.resolve()

How are you testing? Do you have a sample project?

I am using the sample project linked earlier

This is my test

/**
 * Test class for KBuilder.fromPsiType functionality
 */
class SimpleTest : LightJavaCodeInsightFixtureTestCase() {

     fun testCanAccessJava() {
        val type = PsiType.getTypeByName("java.lang.String", project, GlobalSearchScope.allScope(project))
        val result = (type as PsiClassType).resolve()

        assertNotNull(result)
        assertEquals(result!!.qualifiedName, "java.lang.String")
    }
}

And now the .resolve() is returning null.