简体   繁体   中英

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.

Using Visual Studio I used NUnit and TestDriven.net to run my unit tests.

What is a comparable system for Java Using 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.

UPDATE

Okay I didn't know that JUnit was built into the IDE. Are there any plugins that make using JUnit any easier?

Using JUnit with eclipse is actually very easy. Just go to File->New... and select JUnit Test Case. Eclipse will handle adding the JUnit library and all of the imports.

Which version of Eclipse are you using?

For as long as I remember (I've been using Eclipse since early 3.xs), Eclipse supports JUnit out of the box. You just:

Right-click on a project -> 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.

I've also found EclEmma useful for finding untested code.

Easier than "Right-click on a project -> Run As -> JUnit Test"? Like you want it bound to a keypress (because it probably is). Lemme check--Yeah, alt-shift-X, then "T". Easy enough?

There is also window/show view/other/java/JUnit that will give you a junit run bar in a window. 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.

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]"

我已经使用了带有插件的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.

I mean, how much easier can you get than

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

There are also @Before , @After , @BeforeClass , @AfterClass , and @Ignore . The *Class methods need to be static methods. @Before runs before each test, @BeforeClass runs before the first test... but keep in mind that JUnit tests can run in any order.

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.

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

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

many others

in eclipse, you can right click a package and select run as a junit test.

be careful of 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.

What do you mean with "make using JUnit any easier"?

I use Ant for runnings tests as a task. Output will be stored into a flat file or a html file. It depends on the TestRunner.

Please specify your question and you'll get answers! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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