无法在IntelliJ IDEA下运行和调试Groovy测试

14

我尝试将Groovy测试嵌入到Java项目中。 我从spock的示例开始 - https://github.com/spockframework/spock-example

可以通过运行maven goal test编译和执行示例,但是如果我尝试在IntelliJ IDEA下运行测试(在测试方法下按ctrl + F10),它会因为类路径错误而失败。

运行HelloSpockSpec.length of Spock's and his friends' names时出错: 在模块'spock-example'中找不到类'HelloSpockSpec'

我尝试应用IntelliJ + Groovy + Spock的建议,但没有帮助。


该文件夹标记为测试源吗? - tim_yates
你说得对!我忘记做了。谢谢你。如果你的答案正确,我会投票并标记为完成。 - Torsten
完成了!很高兴能够帮助 :-) - tim_yates
2个回答

28
不要忘记在 IntelliJ 中将文件夹标记为“测试源”,然后它应该按预期工作 :-)

4
以防万一,右键点击 test/groovy 文件夹 -> 标记目录为 -> 测试源根目录 - Federico Piazza

5

Intellij可以根据您的pom文件自动将Groovy源代码添加为源目录。在Maven的pom文件中的plugins标签下添加build-helper-maven-plugin配置,指定${basedir}/src/test/groovy作为源目录:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-groovy-test-source</id>
            <phase>test</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${basedir}/src/test/groovy</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接