I'm developing an KSP annotation processor for Kotlin. The code is generated correctly during the compile time and I can see the generated classes in the output directory. Now I want to test my annotation processor via JUnit and "com.github.tschuchortdev.KotlinCompilation". If I call the compile method the code will be generated and I can see the generated class in Temp-Directory but if I try to load the class then I get a "java.lang.ClassNotFoundException: test.pack.TestClassDslBuilder" exception. I hope the code is self-explanatory. My question is: Why isn't the classes compiled and not loadable? Maybe there's a missing configuration of the kompiler.
@BeforeEach
fun setup() {
val kotlinSource = SourceFile.kotlin(
"TestClass.kt", """
package test.pack
import yy.xxx.dsl.builder.annotation.DslBuilder
@DslBuilder
class TestClass {
}
"""
)
val compilation = KotlinCompilation().apply {
sources = listOf(kotlinSource)
symbolProcessorProviders = listOf(DslBuilderProcessorProvider())
//workingDir =
inheritClassPath = true
verbose = false
//messageOutputStream = System.out
kspIncremental = true
}
compilationResult = compilation.compile()
assertEquals(KotlinCompilation.ExitCode.OK, compilationResult.exitCode)
// The next line leads to java.lang.ClassNotFoundException
compilationResult.classLoader.loadClass("test.pack.TestClassDslBuilder")
}
I have the same issue. Didn't fix it, but it seems to me, that problem is in path of generated file (which you wrote like string), like in google compile testing it was important to use StandardLocation.SOURCE_OUTPUT, when you try to find generatedFile(). But in my case, it doesn't generate anything - I mean generatedFiles.size() == 0 in KotlinCompilation.Result