如何设置 Groovy + Eclipse + Junit4?

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

How to setup Groovy + Eclipse + Junit4?

eclipsegroovyjunit

提问by

I am working on a small webapp and I want to use Groovy to write some unit testing for my app. Most of my coding is done on Eclipse and I really want to run all the unit testing with the graphical test runner within Eclipse (I really like the green bar :) )

我正在开发一个小型 web 应用程序,我想使用 Groovy 为我的应用程序编写一些单元测试。我的大部分编码都是在 Eclipse 上完成的,我真的很想在 Eclipse 中使用图形测试运行器运行所有单元测试(我真的很喜欢绿色条:))

Sadly, after 4 hours of try-and-error, I'm still not able to setup properly. I tried to use the Eclipse Junit4 test runner to run a Groovy file with method annotated for testing using @Test. But it keeps complaining NoClassDefFoundException

遗憾的是,经过 4 个小时的尝试和错误,我仍然无法正确设置。我尝试使用 Eclipse Junit4 测试运行器来运行一个 Groovy 文件,其中的方法注释为使用@Test. 但它一直在抱怨NoClassDefFoundException

Anyone can help?

任何人都可以帮忙吗?

Here is content of my groovy file, named simpleTest.groovy

这是我的 groovy 文件的内容,名为 simpleTest.groovy

import org.junit.Test
import static org.junit.Assert.assertEquals

class simpleTest{
  @Test
  void trial(){
    assertEquals 6, 3+3
  }
}

Anyone can help?

任何人都可以帮忙吗?

回答by Robert Munteanu

You might want to give the updated plugin a try, see the the recent blog postfor more details. The theme of this alpha release is providing an optimized edit/save/compile/test experience, which seems to be your exact use case.

您可能想尝试一下更新后的插件,有关更多详细信息,请参阅最近的博客文章。此 alpha 版本的主题是提供优化的编辑/保存/编译/测试体验,这似乎是您的确切用例。

Groovy Eclipse 2.0

Groovy Eclipse 2.0

回答by Peter Kelley

I have this working in my environment so here is a brief summary of what I have:

我在我的环境中有这个工作,所以这里是我所拥有的简要总结:

In the run dialog under JUnit:

在 JUnit 下的运行对话框中:

  • Test Tab:The test class, this must have already been compiled by the Groovy plugin.
  • Classpath:All of the Jar files from my project as well as the Groovy Librarieslibrary
  • Test Tab:测试类,这个必须已经被 Groovy 插件编译过了。
  • 类路径:我项目中的所有 Jar 文件以及Groovy

In Window->Preferences->Java->Build Path

在 Window->Preferences->Java->Build Path

  • Classpath Variables:
    GROOVY_ECLIPSE_HOME
    = the location where the Groovy plugin is installed
  • 类路径变量:
    GROOVY_ECLIPSE_HOME
    = Groovy 插件的安装位置

That does the trick for me.

这对我有用。

回答by Arun R

I had faced a similar issue and it was the missing package statement that caused me to have problems. Groovy Eclipse plugin did not complain about it but my class was present in a package. I got the noClassDefError when running the file as a JUnit Test.

我遇到过类似的问题,是缺少 package 语句导致我遇到问题。Groovy Eclipse 插件没有抱怨它,但我的类存在于一个包中。将文件作为 JUnit 测试运行时,我得到了 noClassDefError。

Adding the package statement to top of class solved this issue.

将 package 语句添加到类的顶部解决了这个问题。

回答by Kivus

Unfortunately, the Groovy Eclipse plugin is pretty horrible at giving actual helpful information to let you know what is going wrong with your setup. I'm going to assume you already did the verification to make sure the plugin is actually building your Groovy files (i.e. doing a sample with no dependencies, checking the properly output directory, etc...) After that, it's a lot of really small configuration verification...I've run into problems where the particular "runner" I'm using in Eclipse (i.e. in the Run menu) doesn't have the write class name defined there or for some reason my project didn't get the JUnit library dependency properly inserted into it.

不幸的是,Groovy Eclipse 插件在提供实际有用的信息以让您知道您的设置出了什么问题方面非常糟糕。我将假设您已经进行了验证以确保该插件实际上正在构建您的 Groovy 文件(即做一个没有依赖项的示例,检查正确的输出目录等......)之后,它真的很多小的配置验证......我遇到了一些问题,我在 Eclipse 中使用的特定“运行器”(即在运行菜单中)没有在那里定义写类名,或者由于某种原因我的项目没有将 JUnit 库依赖项正确插入其中。

Ultimately, it can be a configuration headache, but long term you'll end up saving some time and gaining some cool functionality if you can knock it out...

最终,这可能是一个令人头疼的配置问题,但从长远来看,如果您可以将其淘汰,您最终会节省一些时间并获得一些很酷的功能......