【问题标题】:IDEA Groovy test class already existsIDEA Groovy 测试类已经存在
【发布时间】:2014-11-18 05:28:52
【问题描述】:

IDEA 向我的 groovy 类发出警告“my.class.package”中已存在“类“MyClassTest”。当我运行测试时,它似乎也没有很好地保持课程更新。我将添加一个保证失败或成功的断言,直到稍后它才会识别它(到目前为止似乎是任意的)。鉴于我的 Maven 测试通过并正确运行,我怀疑这只是一个 IDEA 配置问题

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
 <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.xenoterracide</groupId>
<artifactId>rpf</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.9.RELEASE</version>
</parent>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>addSources</goal>
                        <goal>addTestSources</goal>
                        <goal>generateStubs</goal>
                        <goal>compile</goal>
                        <goal>testGenerateStubs</goal>
                        <goal>testCompile</goal>
                        <goal>removeStubs</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>

            </executions>
            <configuration>
                <testSources>
                    <testSource>
                        <directory>${project.basedir}/src/test/groovy</directory>
                        <includes>
                            <include>**/*.groovy</include>
                        </includes>
                    </testSource>
                </testSources>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.8.0.DATAJPA-622-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!--
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1102-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.3.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <!-- use UTF-8 for everything -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <start-class>com.xenoterracide.rpf.Application</start-class>
    <java.version>1.8</java.version>
</properties>


<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <url>http://repo.spring.io/libs-release</url>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>http://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

</project>

【问题讨论】:

    标签: java maven groovy intellij-idea intellij-14


    【解决方案1】:

    我已通过将工件的 target 文件夹标记为“已排除”来解决此问题。

    我认为重复是由target/generated-sources/groovy-stubs 中生成的存根文件引起的。 IDEA 一定对这些文件过于好奇,可能会将它们添加到源集中。

    【讨论】:

    • 这一直有效,直到我运行 clean compile 删除目录并重新创建它...... IntelliJ 再次选择它,就好像我从未将它标记为“排除”一样。您是否遇到过使其始终被排除在外的方法?
    • 我会说这取决于您的构建工具插件(maven,gradle)来排除文件。如果您使用 Maven,您可以应用一些设置来执行此操作(例如 Maven > 导入首选项面板中的“排除构建目录”)
    • @cjstehno,从 gmavenplus 插件中删除 generateStubs 和 testGenerateStubs 目标可以永久解决此问题。抱歉,回复非常延迟:)
    【解决方案2】:

    我遇到了完全相同的问题,将target dir 设置为排除也无济于事。我注意到目录:

    /target/generated-sources/groovy-stubs/test
    

    在 IDEA 中被设置为 Sources Root。我会在 UI 中取消选中它,错误就会消失,但是一旦我重建项目,它就会再次回到它。非常令人沮丧。直到我开始研究所有 gmavenplus-plugin 执行目标。

    原来我列出的目标太多了。如果您只是将 groovy 用于测试目的,就像我一样(使用 Spock - 我已经使用 JUnit),那么您应该只在该插件下启用以下目标:

    addTestSources
    testGenerateStubs
    testCompile
    removeTestStubs
    

    删除其余部分。一旦你这样做了,IDEA 应该对你的 groovy 测试(或者在我的例子中是 Specs)来说都很好。让我知道这是否真的有帮助。

    【讨论】:

      【解决方案3】:

      这是因为 IntelliJ 天生就知道 groovy,您唯一要做的就是不要在 IntelliJ 中激活 gmaveplus-plugin:

      <profiles>
          <profile>
              <id>groovy-integration</id>
              <!-- profile to incorporate gmaveplus in normal build -->
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
      
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.codehaus.gmavenplus</groupId>
                          <artifactId>gmavenplus-plugin</artifactId>
                          <version>1.5</version>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>addSources</goal>
                                      <goal>addTestSources</goal>
                                      <goal>generateStubs</goal>
                                      <goal>compile</goal>
                                      <goal>testGenerateStubs</goal>
                                      <goal>testCompile</goal>
                                      <goal>removeStubs</goal>
                                      <goal>removeTestStubs</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                  </plugins>
              </build>
          </profile>
      
          <profile>
              <id>development-in-idea</id>
              <!-- suppress `groovy-integration` profile by default in IntelliJ -->
              <activation>
                  <property>
                      <name>idea.version</name>
                  </property>
              </activation>
          </profile>
      </profiles>
      

      一个缺点是当您直接从 IntelliJ IDEA 的 Maven 项目工具窗口运行 maven 目标时,groovy-integration 配置文件不会像往常一样被激活。

      但是,您始终可以导航到测试文件夹,单击“Run 'Tests in '...''”并享受 IntelliJ 为您带来的所有好处。

      在 IntelliJ IDEA 中排除 gmavenplus-plugin 具有更多优势,IntelliJ IDEA 有时会将生成的测试存根添加为源而不是测试源,并且由于缺少测试范围的依赖关系而无法编译它们。

      【讨论】:

        【解决方案4】:

        我也收到消息说我的 groovy 类已经存在。我发现它发生在我第一次制作项目之后。从那时起,该错误总是会发生。如果你想摆脱这个错误,你所要做的就是删除存储在 app/build/libs 目录中的 jar 文件。当然,如果你再次制作项目,错误就会回来。因此,在检测该错误时包含它自己的输出 jar 库,这有点让自己绊倒。

        我认为这只是一个令人讨厌的错误,它似乎并没有把事情搞砸。当您没有任何错误时,获得错误指示肯定很烦人。也许有某种方式可以通过某种 groovy lint 选项等来防止这种虚假错误;我只是不知道。我认为排除某些文件(如输出库文件)的 Michel 解决方案会起作用。缺乏关于 groovy 和 gradle 构建的良好、可靠和一致的文档使我对解决这个问题不感兴趣;特别是因为它似乎只是一个令人讨厌的错误。


        这里有一点更新。我确实解决了这个问题,但我不确定你需要做什么来解决它。我能做的最好的就是解释我喝过的东西。我的解决方案似乎以"Class already exists" error in IntelliJ on Groovy class 上发布的解决方案为中心。他们谈论从 IntelliJ 中排除文件。我正在使用 Android Studio 3.1.3,但我无法完全找到一种方法来执行他们在那篇文章中所说的内容。如果我进入项目选项卡并选择项目文件,我可以导航到我的构建文件夹。该目录已被标记为已排除。我确实将它切换为被包含,然后又被排除在外。这似乎并没有解决问题。然后我注意到一个也可以加载/卸载模块。如果我在我的情况下卸载了“应用程序”模块并单击确定,那么关于已经存在的类的错误总是会出现。但是,类文件仍然认为他们是一个错误。换句话说,选项卡中的类文件名在其名称下方仍然有一条曲线。但是请注意,实际的类文件中没有错误行。然后,我尝试了各种卸载和加载模块的尝试,这些模块执行它们位于目录文件结构中的各个其他位置并清理项目。最后,我只是把东西留在了每个模块都被加载的状态。然后我关闭了 Android Studio 项目并重新打开它。我注意到在 Project Files 选项卡中,我无法再像以前那样查看或导航到构建目录。更重要的是,关于已经存在的类的错误消失了。因此,我解决了一系列问题,但直到我关闭 Android Studio 并重新启动它才生效。我注意到在其他情况下,Android Studio 需要关闭并重新启动才能真正纠正自身。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-21
          • 2014-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多