Monday, January 14, 2013

JUnit Testing In Java

This is a little tutorial on how to use JUnit tesing ( JUnit 4.x ) in Eclipse. First you need to add the JUnit library to your project path and import it in order to use it. I'm also going to include an example in this post.

Why use JUnit test

The best reason for testing your code is that you can find your bugs and errors early and you don't have to burden your head later when you have already put it all together. It can save a lot of time in long-term. If you have a lot of code to cover and change it often, testing it is very recommended.


Running your test

Running as a JUnit test with eclpise

Running with code


The Assert class API can be found here.

Item class

Test class


  1. Good example. Unit testing is sign of good developer. For those who are not very familiar with JUnit 4, here is a quick guide of JUnit 4 annotations.

  2. Awesome tutorial! Thank you for the tips!

  3. To find a whole list of test tools other than JUnit which is based on Java Platform and compare the tool functionality and how they match to testing needs can be found in Test Tools Suite