将 Spock 测试添加到 Intellij 中的 Java 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20120153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:17:58  来源:igfitidea点击:

Adding Spock tests to Java project in Intellij

javagroovyintellij-ideaspock

提问by jgm

I have an existing Java project set up in Intellij 12 and am attempting to add some Groovy classes to it. I've started with attempting to add a simple Spock class for testing purposes, but when I right-click on the class it does not give me an option to run it.

我在 Intellij 12 中设置了一个现有的 Java 项目,并且正在尝试向其中添加一些 Groovy 类。我已经开始尝试添加一个简单的 Spock 类以进行测试,但是当我右键单击该类时,它没有提供运行它的选项。

I've taken a look at my Intellij configuration and it pulls in groovy correctly. Further, I can write a Groovy script that uses a Groovy class and that runs without problems so it appears that Groovy is wired in. Is there something else I need to configure to specifically run Spock tests?

我查看了我的 Intellij 配置,它正确地引入了 groovy。此外,我可以编写一个使用 Groovy 类并且运行没有问题的 Groovy 脚本,因此看起来 Groovy 已连接。是否还需要配置其他内容来专门运行 Spock 测试?

回答by Seagull

Easiest way to get spock and all dependencies is add library from maven.

获取 spock 和所有依赖项的最简单方法是从 maven 添加库。

Then, you should place your test in a folder, marked as test folder, if you want to allow batch processing of them.

然后,您应该将您的测试放在一个文件夹中,标记为 test 文件夹,如果您想允许对它们进行批处理。

If you place your script in folder, not marked as test, or source folder, you will be unable to run it.

如果您将脚本放在未标记为测试或源文件夹的文件夹中,您将无法运行它。

If you can't see run button, it looks like Idea cannot recognise file as runnable, it isn't under source/test root, or it's extension is invalid.

如果您看不到运行按钮,则可能是 Idea 无法将文件识别为可运行文件,它不在源/测试根目录下,或者扩展名无效。

回答by Daniil Shevelev

You can add Spock plugin by:

您可以通过以下方式添加 Spock 插件:

  1. Downloading the jar
  2. In IDEA File->Settings->Plugins->Install from disk. Choose the jar.
  1. 下载jar
  2. 在IDEA文件->设置->插件->从磁盘安装。选择罐子。

回答by Jon Peterson

I was having the same issue and ended up here. I found that I had forgotten to extend Specification. As soon as I did, the "Run" option showed up.

我遇到了同样的问题,最后到了这里。我发现我忘记了扩展规范。我一这样做,“运行”选项就出现了。

Just posting in case it helps any other Spock novices like myself.

只是发布以防它可以帮助像我这样的任何其他 Spock 新手。