Groovy Eclipse 无法启动 junit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12781806/
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-09-19 19:09:18  来源:igfitidea点击:

Groovy Eclipse can't launch junit tests

eclipsegroovyjunit

提问by Philippe Blayo

In Eclipse with groovy plugin, I try to launch a test @Test public void testToLaunch() {...}but I have the error:

在带有 groovy 插件的 Eclipse 中,我尝试启动一个测试,@Test public void testToLaunch() {...}但出现错误:

The input type of the launch configuration does not exist

启动配置的输入类型不存在

enter image description here

在此处输入图片说明

Whatinput typeis in the context of launch configuration?(can't find such an entry in launch configuration window)

什么input type是启动配置的情况下?(在启动配置窗口中找不到这样的条目)

Note: I try sts 2.8.1 and e3.7

注意:我尝试 sts 2.8.1 和 e3.7

采纳答案by Anshu

This happens normally when the folder in which test case is present is not a source folder, please check this postas well. Hope that helps!

当测试用例所在的文件夹不是源文件夹时,通常会发生这种情况,也请检查此帖子。希望有帮助!

回答by Aaron L. Johnson

This can also happen if there is a problem with the groovy class. A few things to check:

如果 groovy 类有问题,也会发生这种情况。需要检查的几件事:

1) Ensure that the class name exactly matches the filename (filename = MyTest.groovy)

1) 确保类名与文件名完全匹配 (filename = MyTest.groovy)

package com.mypackage;
import groovy.util.GroovyTestCase;

class MyTest extends GroovyTestCase {}

2) Ensure that the package defined in the file matches the package the file is actually in.

2) 确保文件中定义的包与文件实际所在的包匹配。

回答by 10GritSandpaper

This happened to me, and I just restarted Eclipse (GGTS) and everything was fine again.

这发生在我身上,我刚刚重新启动了 Eclipse (GGTS),一切又恢复了。

回答by Aniket Thakur

In Eclipse you can do

在 Eclipse 中你可以做

Right click ->  properties -> Java build path

Notice test folder is not available in sources. Add it.

注意测试文件夹在源中不可用。添加它。

Add folder -> Select test -> OK

Now rerun you unit test cases.

现在重新运行单元测试用例。

enter image description here

在此处输入图片说明

回答by centic

I had the same error message when I head the test-class duplicated both in the main Java source folder and the testsrc folder. Removing the incorrectly placed one in the main Java source folder solved the problem for me.

当我在主 Java 源文件夹和 testsrc 文件夹中复制重复的测试类时,我收到了相同的错误消息。删除主 Java 源文件夹中错误放置的文件夹为我解决了这个问题。

回答by Robert

This also happened to me. But these tests are written in Groovy. The problem I encountered has to do with how the IDE (Eclipse Kepler, Java EE) first opens a Groovy project after executing "mvn eclipse:eclipse".

这也发生在我身上。但是这些测试是用 Groovy 编写的。我遇到的问题与IDE(Eclipse Kepler,Java EE)在执行“mvn eclipse:eclipse”后如何首先打开一个Groovy项目有关。

The Build Paths do not reference the Groovy source files correctly.

构建路径没有正确引用 Groovy 源文件。

To resolve, I:

为了解决,我:

  1. Right-click on the project, select "Build Path" > "Configure Build Path..."
  2. Select "Source" tab
  3. For test and src folders (.../src/main/groovy, and .../src/test/groovy)
  4. make sure "**/*.groovy" is set as "Inclusion patterns", not "**/*.java"
  1. 右键单击项目,选择“构建路径”>“配置构建路径...”
  2. 选择“来源”选项卡
  3. 对于 test 和 src 文件夹(.../src/main/groovy 和 .../src/test/groovy)
  4. 确保“**/*.groovy”设置为“包含模式”,而不是“**/*.java”

Hope this saves time for someone.

希望这可以为某人节省时间。

Cheers!

干杯!

回答by kcostilow

2019 Update: This drove me crazy for days even with latest Eclipse and fresh installs (Mac, Grails 4, Gradle 5.1.1, Java 8). Some above examples led me to the solution.

2019 年更新:即使使用最新的 Eclipse 和全新安装(Mac、Grails 4、Gradle 5.1.1、Java 8),这也让我疯狂了好几天。上面的一些例子让我找到了解决方案。

My problem was more that the code I was testing included a mix of groovy and java src/main code. It gave me NoClassDefFound on the .groovy classes when I ran my Spec as JUnit.

我的问题更多是我正在测试的代码包含 groovy 和 java src/main 代码的混合。当我将 Spec 作为 JUnit 运行时,它在 .groovy 类上给了我 NoClassDefFound。

Solution: I had to modify my Run/Debug Configuration to include build/classes/groovy/main. Then it worked. It's a little bit of a pain to remember to that for every new Configuration, but, it keeps me going. I hope it helps you.

解决方案:我必须修改我的运行/调试配置以包含 build/classes/groovy/main。然后它起作用了。对于每个新配置,记住这一点有点痛苦,但是,它让我继续前进。我希望它能帮助你。

回答by Akshay Chopra

Whenever you create a Junit test in eclipse, make sure your Junit test file is insidesrc/test/javafolder.

每当您在 Eclipse 中创建 Junit 测试时,请确保您的Junit 测试文件在src/test/java文件夹中。

回答by Akshay Chopra

I had a similar problem. Like others have already pointed out, it was about source folders. I had to change my source folder setup. There was an empty src-folder that disappeared after I right-clicked on it and selected 'remove from build path' from Build path menu. After that I right-clicked both java/src and java/test folders and chose Build path > Use as a source folder. And suddenly my tests were JUnited!

我有一个类似的问题。就像其他人已经指出的那样,它与源文件夹有关。我不得不更改我的源文件夹设置。在我右键单击它并从构建路径菜单中选择“从构建路径中删除”后,有一个空的 src 文件夹消失了。之后,我右键单击 java/src 和 java/test 文件夹并选择 Build path > Use as a source folder。突然间我的测试变成了 JUnited!

In similar situations I'd advice to remove all source folders from build path and add them again when you're sure you've got the right ones. Your source folders should be those with Java package structure under them. In case of proj/java/test/com/stackoverflow/main it's the 'test' folder.

在类似的情况下,我建议从构建路径中删除所有源文件夹,并在您确定拥有正确的文件夹时再次添加它们。你的源文件夹应该是那些在它们下面有 Java 包结构的文件夹。在 proj/java/test/com/stackoverflow/main 的情况下,它是“test”文件夹。

回答by hindolam

This is what resolved for me (Eclipse Oxygen). I had already done what Robert suggested in the earlier post. I was still getting the error. When I went to edit the configuration for junit launch, I saw that the Test Class field just had the class name. I had to hit the Search button to the right. The Test Class field now had the complete name for the class

这就是为我解决的问题(Eclipse Oxygen)。我已经按照罗伯特在之前的帖子中的建议做了。我仍然收到错误消息。当我去编辑 junit 启动的配置时,我看到测试类字段只有类名。我不得不点击右侧的搜索按钮。测试类字段现在具有类的完整名称

com.mycompany.mypackage.MyClass

With this I am able to run the JUnit. But I have to keep fixing this for every run.

有了这个,我就可以运行JUnit. 但我必须在每次运行时不断修复这个问题。