It's the issue of testing component by component, integrate, test again and fix and so ... . Unit testing applies test driven development aspects. If you're familiar with Extreme programming you'll feel it the most suitable for you.
Let start with a simple example describing how to test a simple calculator using JUnit 3.8 and Eclipse IDE 184.108.40.206.
1. Create a new Java Project, name it "JUnit Calculator Test"
2. Create a new class name it "SimpleCalculator" under package "calculator"
package calculator;4. Now we're about to test
public class SimpleCalculator
public static int add( int a , int b )
return a + b;
public static int subtract( int a , int b )
return a - b;
5. Get to your project properties, right click your project and choose properties.
7. Choose "Java Build Path" tab
8. Click "Add library" and Choose "JUnit" from the library list
9. By default you'll find "JUnit 3.8.1" selected leave and proceed till finish.
10. You should have a class path like that. Click OK and be ready to write down your first test case.
13. Now write down this code into the source file
package calculator.test;14. Now we have a test case for both add and subtract methods of our simple calculator.
public class SimpleCalculatorTestCase extends TestCase
public void testAdd()
int added = SimpleCalculator.add( 10 , 12 );
assertEquals( 12 , added );
public void testSubtract()
int subtract = SimpleCalculator.subtract( 12 , 10 );
assertEquals( 2 , subtract );
15. Let's run it. Choose run menu -> Run as -> JUnit Test
16. You should have result like that.
17. When your run a JUnit test case, JUnit automatically parses your test class and find all methods starting with "test" clause, so they are the points to check. So we have two methods testAdd and testSubtract.
18. As you can see testAdd failed, testSubtract succeed. This is because if you check the assertEquals parameters you'll find that the expected parameter is not the really the expected one. But I intended to put it that false to get the idea. The function assertEquals took two parameters.
- The expected value of an expression
- The expression to be evaluated
I think you should now take your hands changing that 12 to 22 and check the results. Waiting for you comments.
Download the full eclipse project here