在 Intellij 下使用 Groovy 测试 Java 代码:无法解析类 GroovyTestCase
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28335101/
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
Testing Java code with Groovy under Intellij: unable to resolve class GroovyTestCase
提问by Patrick Collins
I need to write a short test for some Java code. I used CTRL+SHIFT+T to generate one with IntelliJ, and selected "Groovy JUnit" as the testing library, then wrote the following test:
我需要为一些 Java 代码编写一个简短的测试。我用 CTRL+SHIFT+T 用 IntelliJ 生成一个,并选择了“Groovy JUnit”作为测试库,然后编写了以下测试:
package util
class FibonacciHeapTest extends GroovyTestCase {
FibonacciHeap<Integer> heap
void setUp() {
super.setUp()
heap = new FibonacciHeap<>()
}
void testAddInOrder() {
testForItems 1..1000
}
private void testForItems(Range<Integer> items) {
items.each {heap << it}
assertEquals heap.size, items.to
items.each {assertEquals heap.remove(), it}
}
}
However, when I right click on the test case in the project window, I don't get the "Run All Tests" option that I normally do with JUnit tests, and the compiler throws the following error:
但是,当我在项目窗口中右键单击测试用例时,我没有得到通常对 JUnit 测试执行的“运行所有测试”选项,并且编译器抛出以下错误:
Information:2/4/15 8:15 PM - Compilation completed with 2 errors and 0 warnings in 2 sec
/home/patrick/IdeaProjects/hackerrank/src/test/java/util/FibonacciHeapTest.groovy
Error:(3, 1) Groovyc: unable to resolve class util.FibonacciHeap
Error:(9, 1) Groovyc: unable to resolve class GroovyTestCase
Trying to import GroovyTestCase
or FibonacciHeap
manually causes the same error. IntelliJ does not add any import statements when I let autocomplete finish the names for me, like it usually would with Java code.
尝试导入GroovyTestCase
或FibonacciHeap
手动会导致相同的错误。当我让自动完成为我完成名称时,IntelliJ 不会添加任何导入语句,就像通常使用 Java 代码一样。
What am I doing wrong?
我究竟做错了什么?
回答by banterCZ
You have to configure Groovy SDK first. See the screenshot
您必须先配置 Groovy SDK。看截图
More detailed description in the official document: Configuring Global, Project and Module SDKs
官方文档更详细的描述:Configuring Global, Project and Module SDKs
回答by Bipi
回答by ellpei
I had a similar problem with creating test classes in IntelliJ, and it was solved when creating a new directory outside of the com.company folder (where I had the class I wanted to test).
我在 IntelliJ 中创建测试类时遇到了类似的问题,并且在 com.company 文件夹(我有我想测试的类)之外创建一个新目录时解决了这个问题。
- Create a new directory for the test classes on the same level as your src folder
- Right click on your new test directory, and "Mark directory as" --> "Test Resources Root"
- Now create a test class, which should automatically be added to your test directory.
- 为与 src 文件夹相同级别的测试类创建一个新目录
- 右键单击您的新测试目录,然后“将目录标记为”-->“测试资源根目录”
- 现在创建一个测试类,它应该会自动添加到您的测试目录中。
回答by Pila
In my case, what I did to resolve the issue was rather simple.
就我而言,我为解决问题所做的工作相当简单。
- Close IntelliJ
- Open the attached homepage...
- Remove your project by clicking on the
x
then... - Click on
Import Project
, Navigate to thebuild.graddle
file of your project and open.
- 关闭 IntelliJ
- 打开附带的主页...
- 通过单击
x
然后删除您的项目... - 单击
Import Project
,导航到build.graddle
项目文件并打开。
That was it and all the Red highlightings disappeared.
就是这样,所有红色突出显示都消失了。
回答by Jeffrey Knight
回答by steinybot
As @sman591 pointed out in a comment, if you are getting the error:
正如@sman591 在评论中指出的那样,如果您收到错误消息:
groovyc: unable to resolve class groovy.util.GroovyTestCase
and you already have groovy as a dependency then you are probably just missing the junit dependency.
并且您已经将 groovy 作为依赖项,那么您可能只是缺少 junit 依赖项。
回答by Yan Khonski
In IntelliJ IDEA I re-imported the project. It worked then.
I closed idea. I removed .idea
folder in the project. And I imported the project.
在 IntelliJ IDEA 中,我重新导入了该项目。它当时起作用了。我关闭了想法。我删除.idea
了项目中的文件夹。我导入了项目。
Then I needed to set up Groovy, see previous answers, mark test directory as test source in all modules of my project.
然后我需要设置 Groovy,查看以前的答案,在我的项目的所有模块中将测试目录标记为测试源。