testing_faq.md: add testing jvm projects instructions

This commit is contained in:
Karol Lewandowski 2022-02-09 09:20:52 +01:00
parent f69a04cdf8
commit 20bc9620bc
3 changed files with 34 additions and 14 deletions

View File

@ -82,9 +82,36 @@ Use [`ExtensionTestUtil`](upsource:///platform/testFramework/src/com/intellij/te
Use `com.intellij.util.TimeoutUtil.sleep()`.
### JVM: How to add Maven dependencies?
### How to test a JVM language?
Use [`MavenDependencyUtil`](upsource:///java/testFramework/src/com/intellij/testFramework/fixtures/MavenDependencyUtil.java).
Plugins supporting a JVM language may require JDK and language standard library to be set up in a test project, so the classes like `java.lang.String` can be correctly resolved during tests.
Tests extending [`LightJavaCodeInsightFixtureTestCase`](upsource:///java/testFramework/src/com/intellij/testFramework/fixtures/LightJavaCodeInsightFixtureTestCase.java) use one of the mock JDKs distributed with the [IntelliJ Community project](https://github.com/JetBrains/intellij-community) sources (notice <path>java/mockJDK-$JAVA_VERSION$</path> directories).
These JAR files are not available in plugin project dependencies, so the IntelliJ Community sources must be checked out to the machine running the tests, and sources' location must be provided to the test framework.
It's done by setting the `idea.home.path` system property to the absolute path of the checked out sources.
In projects using Gradle it can be done by providing system property in the `test` task configuration:
```kotlin
test {
systemProperty("idea.home.path", "/path/to/intellij-community")
}
```
The default JDK version used by the test framework depends on the target platform version and is the latest supported version.
The easiest way to change the JDK version to the custom is overriding the `LightJavaCodeInsightFixtureTestCase.getProjectDescriptor()` method and using one of the project descriptors predefined in `LightJavaCodeInsightFixtureTestCase`.
If a project descriptor requires more customizations, its `getSdk()` method can use one of the [`IdeaTestUtil.getMockJdk*()`](upsource:///java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java) methods.
Sometimes testing a JVM language requires adding standard or other libraries to a test project.
If a required library is available in the Maven repository, use [`MavenDependencyUtil`](upsource:///java/testFramework/src/com/intellij/testFramework/fixtures/MavenDependencyUtil.java), e.g.:
```java
MavenDependencyUtil.addFromMaven(model, "org.jetbrains.kotlin:kotlin-stdlib:1.6.10");
```
If a required library is an unpublished JAR file, use [`PsiTestUtil.addLibrary()`](upsource:///platform/testFramework/src/com/intellij/testFramework/PsiTestUtil.java) or `addProjectLibrary()` method and the JAR file path, e.g.:
```java
PsiTestUtil.addLibrary(model, "kotlin-stdlib", getTestDataPath(), "kotlin-stdlib.jar");
```
> If a topic you are interested in is not covered in the above sections, let us know via the "**Was this page helpful?**" feedback form below or [other channels](getting_help.md#problems-with-the-guide).
>

View File

@ -71,6 +71,10 @@ If a plugin supports [multiple platform versions](build_number_ranges.md), it mu
See [Dependency on the standard library](https://kotlinlang.org/docs/gradle.html#dependency-on-the-standard-library) for more details.
> If you need to add Kotlin Standard Library to your **test project** dependencies, see the [](testing_faq.md#how-to-test-a-jvm-language) section.
>
{type="tip"}
## Kotlin Gradle Plugin
Plugins using the [Gradle Build System](gradle_build_system.md) use the [Kotlin JVM Gradle plugin](https://kotlinlang.org/docs/gradle.html#targeting-the-jvm).

View File

@ -27,15 +27,4 @@ Under <path>test</path>, create the <path>java</path> folder for test source cod
## Set the Run Configuration Parameters
Because some of the tests use Java files as test data, the tests need to mock up the project's SDK.
The testing framework uses <path>java/mockJDK-$JAVA_VERSION$</path> from IntelliJ Community sources by default when using [`LightJavaCodeInsightFixtureTestCase`](upsource:///java/testFramework/src/com/intellij/testFramework/fixtures/LightJavaCodeInsightFixtureTestCase.java) test base class.
The system properties are defined in the <path>build.gradle</path> file using the snippet shown below.
The <path>/path/to/intellij-community/</path> is set to the absolute path to the local [IntelliJ Community project](https://github.com/JetBrains/intellij-community) source's root directory on the machine running the tests.
For example, on macOS it might be <path>/Users/$USER_NAME$/Documents/intellij-community/</path>
```groovy
test {
systemProperty "idea.home.path", "/path/to/intellij-community/"
}
```
Because some tests use Java files as test data, the tests need to mock up the project's SDK. See the [](testing_faq.md#how-to-test-a-jvm-language) section for details.