How to create unit tests easily in eclipse

EclipseUnit TestingTestingJunit

Eclipse Problem Overview


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 Solutions


Solution 1 - Eclipse

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.

Solution 2 - Eclipse

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.

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

Solution 3 - Eclipse

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

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

Hope that helps.

Solution 4 - Eclipse

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.

Close to 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.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChotkaView Question on Stackoverflow
Solution 1 - Eclipse卢声远 Shengyuan LuView Answer on Stackoverflow
Solution 2 - EclipsefastcodejavaView Answer on Stackoverflow
Solution 3 - EclipseNileshView Answer on Stackoverflow
Solution 4 - Eclipsedj_segfaultView Answer on Stackoverflow