Kotlin Compile Testing Project Overview
What is Kotlin Compile Testing?
Kotlin Compile Testing is an innovative library designed to simplify and enhance the process of testing Kotlin and Java code compilations within the same environment. Inspired by Google's Compile Testing framework, this tool allows developers to test annotation processors or compiler plugins more efficiently. By providing an integrated compilation environment, it enables the testing of complex Kotlin and Java code interactions seamlessly.
Key Use Cases
Kotlin Compile Testing is particularly valuable for:
- Compiling Code in Tests: It allows the simultaneous compilation of Kotlin and Java code, facilitating unit tests that require these languages to interact.
- Testing Annotation Processors: Developers can test custom annotation processors.
- Evaluating Compiler Plugins: It provides a platform to test various compiler plugins.
- Assessing Kotlin Code Generation: The library can be used to test and validate Kotlin code generation processes.
Example Usage
The project offers a detailed example of its functional capacity:
-
Create Source Files: Developers start by creating Kotlin and Java source files within their test environment.
-
Configure Compilation: A
KotlinCompilation
object is set up, where sources are added alongside any custom annotation processors or compiler plugins required for the test. -
Assert Results: The compilation results can be asserted using standard testing assertions to check for correct exit codes or specific message outputs, providing feedback on diagnostics and other compilation outcomes.
Features
Kotlin Compile Testing distinguishes itself with several robust features:
- Mixed-Source Compilation: This feature allows for the simultaneous compilation of both Kotlin and Java file sets.
- Advanced Annotation Processing: Users can run, debug, and control annotation processors across both languages, leveraging the single-process nature of the library for easier debugging.
- Classpath Inheritance: Compiled sources can inherit and utilize the classpath of the application under test.
- Jigsaw Compatibility: It supports JDK 8 and later, aligning with Java's module system.
- Cross-Compilation: Developers can specify a JDK for compiling the code, enhancing compatibility testing across different Java versions.
- Automatic Dependency Resolution: The library can automatically detect necessary dependencies on the host classpath.
Installation
To incorporate Kotlin Compile Testing into a project, developers can add a dependency directly from Maven Central in their build.gradle
file:
dependencies {
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.5.0")
}
Compatible Compiler Versions
It supports any local Kotlin compiler version, but there must be compatibility between the specific version of kotlin-compiler-embeddable
used by the project and the library to avoid dependency conflicts.
Kotlin Symbol Processing API Support
For projects using the Kotlin Symbol Processing (KSP) API, Kotlin Compile Testing offers dedicated support. Developers can test KSP processors by including the KSP-specific dependencies and specifying symbol processor providers in their tests.
Examples of Usage in Projects
Numerous projects across the development landscape utilize Kotlin Compile Testing, including high-profile ones like androidx/room, google/dagger, and square/moshi.
Java 16 Compatibility
The Kotlin Compile Testing project has adapted to changes brought about by Java's Jigsaw module system, especially pertaining to Java 16. Developers might need to adjust their build configurations to handle access control requirements introduced by the Jigsaw system.
Summary
Kotlin Compile Testing stands out as a versatile and comprehensive solution for developers looking to streamline the process of testing complex Kotlin and Java code in unison. Its robustness in handling annotation processors, compiler plugins, and both simple and advanced compilation scenarios make it a valuable asset in any developer's toolkit. With strong community and industry adoption, it continues to evolve and support the latest advances in Kotlin and Java development environments.