Intellij plugin development & Kotlin tests - kotlin

I'm trying to develop an intellij plugin written in kotlin - I'm struggling to find any examples of unit tests for the plugin that are written in kotlin, or even java ones with test data that are written using gradle rather than the older dev SDK. Is anyone able to point to a demo application with unit tests? (For inspections, ideally)

Some theory:
Testing in Gradle
Add test classes to a Gradle project
Testing Gradle plugins
Running JUnit5 tests with Gradle
Unit testing in Kotlin and kotlin.test library
Using Gradle with Kotlin
Code samples:
Article on running Kotlin Unit tests with Gradle
A tutorial on building apps with Spring Boot and Kotlin with a section on testing with Junit5
Custom Gradle plugin example
Hope this helps.

These come out of the box now if you use the template: https://github.com/JetBrains/intellij-platform-plugin-template
Here's one of the tests in Kotlin:
package org.jetbrains.plugins.template
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.psi.xml.XmlFile
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.util.PsiErrorElementUtil
class MyPluginTest : BasePlatformTestCase() {
fun testXMLFile() {
val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "<foo>bar</foo>")
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile))
assertNotNull(xmlFile.rootTag)
xmlFile.rootTag?.let {
assertEquals("foo", it.name)
assertEquals("bar", it.value.text)
}
}
}
The plugin documentation also provides more relevant info for testing: https://plugins.jetbrains.com/docs/intellij/writing-tests.html
Also, here are some unit tests written in Java for the JUnit IntelliJ plugin https://github.com/JetBrains/intellij-community/blob/master/plugins/junit/test/com/intellij/execution/junit/JUnitDetectionTest.java

Related

What is the gradle command to run scenarios with tags?

I am using Gradle 7.6, Karate 1.3.1, Java 17.0.5 and Junit 5.8.1.
I want to configure a Jenkin job for each feature to create a health check monitor. I need gradle commands to run feature files using tags #smoke, #regression, #featureName etc.,
I have tried with the following command, it worked earlier and stopped working recently.
./gradlew test -Dkarate.options="--tags #smoke" -Dtest.single=TestRunner#testTagsWithoutFeatureName
Where TestRunner is the following Java class
import com.intuit.karate.junit5.Karate;
public class TestRunner {
#Karate.Test
Karate testTagsWithoutFeatureName() {
return Karate.run().tags("#smoke").relativeTo(getClass());
}
}
My advice is use the Runner class, that is better designed for running tests in CI. The JUnit helpers are just for local-dev convenience: https://stackoverflow.com/a/65578167/143475
It should be possible to even pass a feature to karate.options as the last argument. Which might be more convenient than writing a Java class for every combinations. You should experiment.
Otherwise no suggestions, but if you feel there's a bug, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

With Gradle jvm-test-suite, the integrationTest suite can not access kotlin internal Apis

I am trying to use the gradle JVM test suite plugin to perform integration test for my Kotlin project. Some of the classes and apis are internal visibility.
I followed the sample code to create new test suite integrationTest:
testing {
suites {
val integrationTest by registering(JvmTestSuite::class) {
dependencies {
implementation(project)
}
...
}
}
}
But the source code under src/integrationTest/kotlin could not see internal classes in project main. I understand that the test suite of integrationTest is not in the same module with project main.
My questions are:
why this behavior is not consistent with the default test suite test which can access internal classes in project main?
If this is by design, how can I workaround this to make internal classes visible to test suite of integrationTest?
Had the same problem. Found an answer here: Adding an additional test suite to Gradle seems you need to add
compileClasspath += sourceSets["main"].runtimeClasspath

How to make Karate Tests run as a service [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751

How to setup a run configuration for Ktor in IntelliJ IDEA?

I try to add a run configuration in IntelliJ IDEA for my Ktor app using their manual but it doesn't work:
And that's true, io.ktor.server.netty.EngineMain is really not in my module. However, how to run it via IntelliJ IDEA? I added a configuration for Gradle but it seems an ugly way.
I suggest adding main function with the following code:
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
and running it via Kotlin run/debug configuration.

Runner.runFeature can't find the feature file path - karate 0.9.9.RC3-junit5 [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751