简体   繁体   English

Java 注释处理器 - 带注释的 Kotlin 类单元测试

[英]Java annotation processor - Annotated Kotlin classes unit tests

I have an annotation processor library that I would like to get working for Kotlin, however I have hit a snag in terms of my unit testing.我有一个注释处理器,我想为 Kotlin 工作,但是我在单元测试方面遇到了障碍。 I would really appreciate if someone could give me some advice.如果有人能给我一些建议,我将不胜感激。

My current unit testing implementation uses Google's compile-testing library.我当前的单元测试实现使用 Google 的编译测试库。 I create input and output classes and store them in the resources directory.我创建输入和输出类并将它们存储在资源目录中。 Then during the unit test, the compile-testing library compiles the input java class, executes the annotation processor and then compares the generated classes against the expected output class from the resources directory.然后在单元测试期间,编译测试库编译输入的 java 类,执行注解处理器,然后将生成的类与资源目录中的预期输出类进行比较。

Here is an example (from my project) of what I am referring to: Unit test class Resources (Input and expected output classes这是我所指的一个示例(来自我的项目): 单元测试类资源(输入和预期输出类

This is working great for all my current java based unit tests.这对我当前所有基于 java 的单元测试都很有用。 However when I attempt to write some tests using Kotlin classes, my test fails to load the class.但是,当我尝试使用 Kotlin 类编写一些测试时,我的测试无法加载该类。

I believe this is due to the compile-testing library being first and foremost a Java specific library (I don't see any mention of Kotlin on their project)我相信这是因为编译测试库首先是一个 Java 特定的库(我在他们的项目中没有看到任何提及 Kotlin 的内容)

At the moment I get the following issue:目前我遇到以下问题:

java.lang.IllegalArgumentException: Compilation unit is not of SOURCE kind: "/C:/dev/gsonpath/gsonpath-compiler/build/resources/test/adapter/auto/field_types/primitives/valid/TestValidPrimitives.kt"

at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:137)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:107)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:64)
at com.google.testing.compile.Compilation.compile(Compilation.java:69)
at com.google.testing.compile.JavaSourcesSubject$CompilationClause.compilesWithoutError(JavaSourcesSubject.java:281)

The problem is fairly obvious that the incorrect compiler is being used.问题很明显,因为使用了不正确的编译器。 The exception itself is thrown when my file extension is not '.java'.当我的文件扩展名不是“.java”时,会抛出异常本身。 If I attempt to load a Kotlin class with the '.java' file extension, it doesn't work since it is not correct Java syntax.如果我尝试加载带有“.java”文件扩展名的 Kotlin 类,它不起作用,因为它不是正确的 Java 语法。

Has anyone come across this issue before and solved it?有没有人以前遇到过这个问题并解决了它? I have had a look at a few other annotation processors (such as DBFlow ), and they do not write unit tests in this manner.我看过其他一些注释处理器(例如DBFlow ),它们不会以这种方式编写单元测试。

Since Kotlin is only recently dabbling in annotation processing, perhaps I am the first to have this problem?由于Kotlin最近才涉足注解处理,也许我是第一个遇到这个问题的人?

Kotlin integrates with ordinary Java annotation processors by generating "stubs" (empty class carcasses, that have same semantic/methods/fields as target Kotlin classes) [1] . Kotlin通过生成“存根”(空类胴体,与目标Kotlin类具有相同的语义/方法/字段)来集成普通的Java注释处理器[1] This means, that unit-testing Java annotation processors with kapt is essentially impossible — even if you somehow integrate the Kotlin tooling in your testing flow, you will end up testing kapt itself, rather then your own code. 这意味着,使用kapt对Java注释处理器进行单元测试基本上是不可能的 - 即使您以某种方式将Kotlin工具集成到测试流程中,您最终将测试kapt本身,而不是您自己的代码。

If you want to ensure, that your annotation processing code works with Kotlin-generated stubs, just generate all possible invariants that may be produced by stub generator, and use those as test subjects like any ordinary Java code. 如果你想确保你的注释处理代码与Kotlin生成的存根一起工作,只需生成可能由存根生成器生成的所有可能的不变量,并将它们用作测试主题,就像任何普通的Java代码一样。

Old question but kotlin-compile-testing is now available and widely used for this usage.老问题,但kotlin-compile-testing现在可用并广泛用于此用途。 It supports java and kotlin files as well.它也支持 java 和 kotlin 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java / Kotlin注释处理器:获取带注释的字段/属性的类型 - Java/Kotlin annotation processor: get type of annotated field/property Java,在编译时使用Annotation Processor进行索引注释类是一种很好的做法吗? - Java, Is index annotated classes at compile time with Annotation Processor a good practice? 如何为 Java 注释处理器编写自动化单元测试? - How to write automated unit tests for java annotation processor? Java注释处理器是否能够删除注释的代码 - Is Java annotation processor capable of removing annotated code Java 11-Kotlin注解处理器 - Java 11-Kotlin annotation processor 使用Java注释处理器查找带注释方法的方法参数? - Find Method Arguments of annotated Method using Java Annotation Processor? 如何只允许在 Java 中的方法参数中使用某些注释进行注释的类? - How to allow only classes annotated with some annotation in method parameters in Java? 如何确定注释处理器中被注释元素的类路径? - How to determine the classpath of an annotated element in an annotation processor? 过滤带有特定注释的类 - Filter classes that are annotated with a specifi annotation 如何在 Java 11 的注释处理器中获取带注释的方法参数的名称 - How can I get the names of annotated method parameters in an Annotation Processor in Java 11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM