简体   繁体   English

你需要做什么用Java在Java中进行单元测试?

[英]What do you need to do Unit testing in Java with Eclipse?

I've recently been lifted out of the .Net world into the Java world and I miss my unit tests. 我最近被从.Net世界升级到Java世界,我想念我的单元测试。

Using Visual Studio I used NUnit and TestDriven.net to run my unit tests. 使用Visual Studio我使用NUnit和TestDriven.net来运行我的单元测试。

What is a comparable system for Java Using Eclipse? 什么是Java使用Eclipse的可比系统?

I'm looking specifically for the plugins that will get me going, or a guide on how to do it. 我正在寻找能让我前进的插件,或者是如何做到这一点的指南。

I'm aware that JUnit is what NUnit was initially based on, but I want to know the best way to integrate it into Eclipse as there seem to be a few plugins that do this and I don't have the time to play around with them all. 我知道JUnit是NUnit最初的基础,但是我想知道将它集成到Eclipse中的最佳方法,因为似乎有一些插件可以做到这一点而且我没有时间玩商场。

UPDATE UPDATE

Okay I didn't know that JUnit was built into the IDE. 好吧,我不知道JUnit是内置于IDE中的。 Are there any plugins that make using JUnit any easier? 是否有任何使用JUnit的插件更容易?

Using JUnit with eclipse is actually very easy. 将JUnit与eclipse一起使用实际上非常简单。 Just go to File->New... and select JUnit Test Case. 只需转到File-> New ...并选择JUnit Test Case。 Eclipse will handle adding the JUnit library and all of the imports. Eclipse将处理添加JUnit库和所有导入的问题。

Which version of Eclipse are you using? 您使用的是哪个版本的Eclipse?

For as long as I remember (I've been using Eclipse since early 3.xs), Eclipse supports JUnit out of the box. 只要我记得(我从早期的3.xs开始就使用Eclipse),Eclipse支持开箱即用的JUnit。 You just: 你刚才:

Right-click on a project -> Run As -> JUnit Test 右键单击项目 - > Run As - > JUnit Test

Does this not work for you? 这不适合你吗?

I've been using moreUnit for a few years and can't live without its Ctrl + J shortcut to switch between the class and its test case. 我已经使用moreUnit几年了,如果没有它的Ctrl + J快捷方式在类和它的测试用例之间切换,我就无法生存。

I've also found EclEmma useful for finding untested code. 我还发现EclEmma对于查找未经测试的代码非常有用。

Easier than "Right-click on a project -> Run As -> JUnit Test"? 比“右键单击项目 - >运行方式 - > JUnit测试”更容易? Like you want it bound to a keypress (because it probably is). 就像你想要它绑定到按键(因为它可能是)。 Lemme check--Yeah, alt-shift-X, then "T". Lemme check - 是的,alt-shift-X,然后是“T”。 Easy enough? 够容易吗?

There is also window/show view/other/java/JUnit that will give you a junit run bar in a window. 还有窗口/显示视图/其他/ java / JUnit,它将在窗口中为您提供一个junit运行栏。 You can then just hit the run tests button and it will run all the tests in your project/section. 然后,您可以点击运行测试按钮,它将运行项目/部分中的所有测试。

Ctrl-shift-L is great for figuring out keybindings if you are getting to know eclipse. 如果您要了解eclipse,Ctrl-shift-L非常适合计算键绑定。

Also, get VERY familiar wtih ctrl-space, just press it whenever you're in the middle of typing something (seriously, try it with everything!) Also type "sysout[ctrl-space]" 另外,通过ctrl-space得到非常熟悉,只要你在打字的时候按下它(严肃地说,尝试一切!)同时输入“sysout [ctrl-space]”

我已经使用了带有插件的testNG用于eclipse

JUnit 4 is actually really easy to use, as long as you're using a project that targets Java 5 or newer, and have added it to the project. JUnit 4实际上非常容易使用,只要您使用的是针对Java 5或更新版本的项目,并将其添加到项目中。

I mean, how much easier can you get than 我的意思是,你能获得多少容易

@Test
public myTest() {
    // Test code here
}

There are also @Before , @After , @BeforeClass , @AfterClass , and @Ignore . 还有@Before@After@BeforeClass@AfterClass@Ignore The *Class methods need to be static methods. * Class方法需要是静态方法。 @Before runs before each test, @BeforeClass runs before the first test... but keep in mind that JUnit tests can run in any order. @Before运行每次测试之前, @BeforeClass第一次测试之前运行......但是请记住,JUnit测试可以按任意顺序运行。

If you're doing database tests, you may want to look into the DBUnit addon, although you need to take some special steps to use it with JUnit 4's native mode. 如果您正在进行数据库测试,您可能需要查看DBUnit插件,尽管您需要采取一些特殊步骤将其与JUnit 4的本机模式一起使用。

fit ( http://www.theserverside.com/news/thread.tss?thread_id=39417 ) 适合( http://www.theserverside.com/news/thread.tss?thread_id=39417

dbunit ( http://www.dbunit.org/ ) dbunit( http://www.dbunit.org/

many others 很多其他的

in eclipse, you can right click a package and select run as a junit test. 在eclipse中,您可以右键单击一个包并选择run作为junit测试。

be careful of http://xunitpatterns.com/test%20fixture%20-%20ambiguous.html . 小心http://xunitpatterns.com/test%20fixture%20-%20ambiguous.html iirc, this boils down to junit creating an instance of each test case before calling setup and nunit just creating one instance. iirc,这归结为junit在调用setup之前创建每个测试用例的实例,nunit只创建一个实例。

What do you mean with "make using JUnit any easier"? “使用JUnit变得更容易”是什么意思?

I use Ant for runnings tests as a task. 我将Ant用于运行测试作为一项任务。 Output will be stored into a flat file or a html file. 输出将存储到平面文件或html文件中。 It depends on the TestRunner. 这取决于TestRunner。

Please specify your question and you'll get answers! 请指出你的问题,你会得到答案! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM