首页 > 解决方案 > kapt 生成的文件包含错误并阻止从 IntelliJ 调试单元测试

问题描述

我有一个项目正在尝试进行单元测试。

测试运行良好。为了简洁起见,以下代码是一个虚拟测试:

enum class QueryFilterOperator(val operator: String) {
    EQUAL("="),
    GREATER_THAN(">"),
    LOWER_THAN("<"),
    GREATER_THAN_OR_EQUAL(">="),
    LOWER_THAN_OR_EQUAL("<=");
}

class QueryFilterOperatorTest : StringSpec({
    "a query filter operator" {
        assertEquals("=", EQUAL.operator)
    }
})

但是当尝试使用 IntelliJ 工具对其进行调试时,在一些 kapt 生成的代码上编译失败:

package com.wizy.data.contracts;

import java.lang.System;

@kotlin.Metadata(mv = {1, 1, 16}, bv = {1, 0, 3}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0010\n\u0000\n\u0002\u0010\u000e\n\u0002\b\t\b\u0086\u0001\u0018\u00002\b\u0012\u0004\u0012\u00020\u00000\u0001B\u000f\b\u0002\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006j\u0002\b\u0007j\u0002\b\bj\u0002\b\tj\u0002\b\nj\u0002\b\u000b\u00a8\u0006\f"}, d2 = {"Lcom/wizy/data/contracts/QueryFilterOperator;", "", "operator", "", "(Ljava/lang/String;ILjava/lang/String;)V", "getOperator", "()Ljava/lang/String;", "EQUAL", "GREATER_THAN", "LOWER_THAN", "GREATER_THAN_OR_EQUAL", "LOWER_THAN_OR_EQUAL", "data-contracts"})
public enum QueryFilterOperator {
    /*public static final*/ EQUAL /* = new EQUAL(null) */,
    /*public static final*/ GREATER_THAN /* = new GREATER_THAN(null) */,
    /*public static final*/ LOWER_THAN /* = new LOWER_THAN(null) */,
    /*public static final*/ GREATER_THAN_OR_EQUAL /* = new GREATER_THAN_OR_EQUAL(null) */,
    /*public static final*/ LOWER_THAN_OR_EQUAL /* = new LOWER_THAN_OR_EQUAL(null) */;
    @org.jetbrains.annotations.NotNull()
    private final java.lang.String operator = null;

    @org.jetbrains.annotations.NotNull()
    public final java.lang.String getOperator() {
        return null;
    }

    QueryFilterOperator(java.lang.String operator) {
    }
}

在这段代码中,我们可以看到枚举构造函数设置不正确。

如果我删除构造函数并重试(使用公认的更笨的测试),我可以按预期进行调试。

enum class QueryFilterOperator {
    EQUAL,
    GREATER_THAN,
    LOWER_THAN,
    GREATER_THAN_OR_EQUAL,
    LOWER_THAN_OR_EQUAL;
}

class QueryFilterOperatorTest : StringSpec({
    "a query filter operator" {
        assertNotEquals(EQUAL, GREATER_THAN)
    }
})

为什么 kapt 会这样?为什么只有调试的时候才会报错?为什么它甚至会生成这个明显不正确的 java 代码?

这是 POM 中的 kapt 配置:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>1.3.61</version>
    <executions>
        <execution>
            <id>kapt</id>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths/>
            </configuration>
        </execution>
        <execution>
            <id>compile</id>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <source>${basedir}/src/main/java</source>
                    <source>${basedir}/target/generated-sources/annotations</source>
                </sourceDirs>
            </configuration>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <jvmTarget>1.8</jvmTarget>
    </configuration>
</plugin>

标签: kotlinintellij-ideakapt

解决方案


嗯,在清除所有 IntelliJ 项目文件(*.iml文件和.idea目录)后,一切都运行良好,即使在调试中也是如此。


推荐阅读