IDEA Groovy test class already exists

人走茶凉 提交于 2021-01-18 13:53:39

问题


IDEA is giving my groovy class the warning `class "MyClassTest" already exists in "my.class.package". It also doesn't seem to be doing a very good job of keeping the class updated when I run the test. I'll add an assertion guaranteed to fail, or succeed and it won't recognize it until later (later so far seems arbitrary). Given that I have maven tests passing and running correctly I suspect this is simply an IDEA configuration problem

here's my 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>

回答1:


I've fixed the issue by marking the target folder of the artifact as "Excluded".

I think the duplication is caused by the stub files generated in target/generated-sources/groovy-stubs. IDEA must be too curious about these files and might add them to the source set.




回答2:


I had the exact same problem and setting target dir as excluded didn't help either. I noticed that the directory:

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

was getting set as a Sources Root in IDEA. I'd uncheck that in the UI and the error would go away, but as soon as I'd rebuild project it was back to it again. Very frustrating. Until I started looking at all the gmavenplus-plugin execution goals.

It turned out I had a few too many goals listed. If you're only using groovy for testing purposes, like I am (with Spock - I'm so done with JUnit), you should only have the following goals enabled under that plugin:

addTestSources
testGenerateStubs
testCompile
removeTestStubs

Remove the rest of them. Once you do that IDEA should be all good with your groovy tests (or Specs in my case). Let me know if that really helped.




回答3:


This is because of IntelliJ knows groovy by nature, the only thing you have to do is DO NOT activate gmaveplus-plugin in IntelliJ:

<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>

One down side is that when you run maven goal from IntelliJ IDEA's Maven Projects tool window directly, the groovy-integration profile will not be activated as usual.

However, you can always navigate to the test folder, click "Run 'Tests in '...''" and have all the benefits brought to you by IntelliJ.

Exclude gmavenplus-plugin within IntelliJ IDEA takes one more advantage, IntelliJ IDEA sometimes add the generated test stubs as Sources instead of Test Sources, and unable to compile them due to lack of test-scoped dependencies.




回答4:


I too get the message saying that my groovy class already exists. What I discovered is that it occurs after I Make Project the first time. From then on that error always occurs. If you want to get rid of the error, all you have to do is to delete the jar file produced stored in the app/build/libs directory. And of course, if you re Make Project again, the error comes back. So it's kind of tripping over itself by including it's own output jar lib in detecting that error.

I think its just a nuisance error and it doesn't seem to mess things up. It's certainly annoying to get an error indication when you don't have anything wrong. Perhaps there's someway to prevent this bogus error with some kind of groovy lint option, etc.; I just don't know. I would think that Michel solution of excluding certain files like the output lib file would work. The lack of good, reliable and consistent documentation concerning groovy and gradle builds makes me not interested in wanting to solve this problem; especially since it appears to just be a nuisance error.


Here's a little update. I did solve the problem, but I'm not exactly sure what the exact sequence you need to do to solve it. The best I can do is to explain the things that I dinked with. My solution seems to be centered around the solution posted at "Class already exists" error in IntelliJ on Groovy class . They talk about excluding a file from IntelliJ. I'm using Android Studio 3.1.3 and I couldn't exactly find a way to do what they where saying in that post. If I went into the Project tab and seletected Project Files I was able to navigate to my build folder. That directory was already marked as excluded. I did toggle it to being included and then back to being excluded. That did not seem to fix the problem. I then noticed that one could also load/unload modules. If I unloaded in my case the 'app' module and clicked okay, the error about the class already existing went always. However, the class file still thought their was an error. In other words, the class file name in the tab still had a squiggle line underneath it's name. Note however, there were no lines in error in the actually class file. I then played around with various attempts of unloading and loading of Modules executing them positioned at various other places in the directory file structure and cleaning the project. At the end, I just left things in the state where every module was loaded. I then closed the Android Studio project and reopened it. What I noticed was that the in Project Files tab, I could no longer see or navigate to the build directory as I previously could. And more importantly, the error about the class already existing went away. So something in the sequence of things I dinked with fixed the problem, but it didn't take affect until I closed Android Studio and restarted it. I've noticed in other situations where Android Studio need to be close and restarted to actually correct itself.



来源:https://stackoverflow.com/questions/26987237/idea-groovy-test-class-already-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!