如何在 Eclipse 中轻松创建单元测试

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

How to create unit tests easily in eclipse

eclipseunit-testingtestingjunit

提问by Chotka

I want to create unit tests easily by just selecting method. Is there a tool in eclipse that does that. It should support templates. I should be able to create positive test as well as negative tests.

我想通过选择方法轻松创建单元测试。eclipse 中是否有一个工具可以做到这一点。它应该支持模板。我应该能够创建正面测试和负面测试。

采纳答案by fastcodejava

You can use my plug-in to create tests easily:

您可以使用我的插件轻松创建测试:

  1. highlight the method
  2. press Ctrl+Alt+Shift+U
  3. it will create the unit test for it.
  1. 突出方法
  2. Ctrl+ Alt+ Shift+U
  3. 它将为其创建单元测试。

The plug-in is available here. Hope this helps.

该插件可在此处获得。希望这可以帮助。

回答by u290629

To create a test case template:

创建测试用例模板:

"New" -> "JUnit Test Case" -> Select "Class under test" -> Select "Available methods". I think the wizard is quite easy for you.

“新建”->“JUnit 测试用例”-> 选择“被测类”-> 选择“可用方法”。我认为向导对你来说很容易。

回答by Nilesh

Check out this discussion [How to automatically generate junits?]

查看此讨论[如何自动生成 junit?]

If you are starting new and its a java application then Spring ROOlooks very interesting too!

如果您开始使用新的 Java 应用程序,那么Spring ROO看起来也很有趣!

Hope that helps.

希望有帮助。

回答by dj_segfault

Any unit test you could create by just pressing a button would not be worth anything. How is the tool to know what parameters to pass your method and what to expect back? Unless I'm misunderstanding your expectations.

只需按一个按钮就可以创建的任何单元测试都没有任何价值。该工具如何知道要传递您的方法的参数以及期望返回的内容?除非我误解了你的期望。

Closeto that is something like FitNesse, where you can set up tests, then separately you set up a wiki page with your test data, and it runs the tests with that data, publishing the results as red/greens.

关闭到有点像FitNesse的,在这里你可以设置测试,然后分别设置了一个wiki页面,您的测试数据,它运行与数据测试,公布结果为红/绿色。

If you would be happy to make test writing much faster, I would suggest Mockito, a mocking framework that lets you very easily mock the classes around the one you're testing, so there's less setup/teardown, and you know you're really testing that one class instead of a dependent of it.

如果您愿意更快地编写测试,我会建议Mockito,这是一种模拟框架,可让您非常轻松地模拟您正在测试的类周围的类,因此设置/拆卸更少,并且您知道您真的测试一个类而不是它的依赖项。