Tuesday, June 12, 2007

JUnit - The ever easier

Really how amazing and powerful is that framework. JUnit is in role one of the most well-known unit testing frameworks for Java.

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 3.2.2.1.

1. Create a new Java Project, name it "JUnit Calculator Test"



2. Create a new class name it "SimpleCalculator" under package "calculator"



3. Write down those two sample functions
package calculator;

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;
}

}
4. Now we're about to test

5. Get to your project properties, right click your project and choose properties.



6. Now you should have something like that

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.



11. Right click you project select new -> "JUnit Test Case"



12. Name it SimpleCalculatorTestCase
13. Now write down this code into the source file
package calculator.test;
import calculator.SimpleCalculator;
import junit.framework.TestCase;

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 );
}

}

14. Now we have a test case for both add and subtract methods of our simple calculator.
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
If the two are equals so it means the the test is passed.

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

References
http://en.wikipedia.org/wiki/Junit

12 comments:

Beroz said...

nice post :)!

Ibrahim said...

Good work Mohammed Continue and Wishing good future

Mohammad Abou-Basha said...

You're welcome :)

هند عبدالخالق said...

جزاكم الله خيرا

هند عبدالخالق said...

جزاكم الله خيرا
هذا أفضل من اختبار الوحدات يدويا

Ayman Emam said...

dear mido:

nice work, but i would suggest for you to make episodes for this tutorials, as for people with much higher skills, they see this tutorials are for novices, to attract more people try to make a variety for both levels try to make code to solve hard problems and tutorials for introduction, any way nice work.

The most important thing keep good work, don't stop.

Mohammad Abou-Basha said...

Ayman, your idea is very welcomed, I'm already preparing for an advanced Swing Tutorial but I'm asking Allah to provide the time for it. Any way thanks for you promoting comments, keep on.

Hosam Aly said...

Nice idea. "Simply Elegant, Elegantly Simple" as Nokia people say. :D

How about making it even easier? Did you know that you did not have to modify your build path manually?

In any Java project, you can simply ask to create a new JUnit Test Case, and Eclipse will suggest to add JUnit libraries to your project (if the project does not already include them). You'll find a link to do that at the bottom of the New JUnit Test Case dialog.

Waiting for more useful articles. :)

Mohammad Abou-Basha said...

Thanks for your comment Hossam :). Concerning the issue of Build Path I'll reflect it isA ;).

MAGA said...

Hi Abou,

Good Work Keep it up..My suggestion is keep on updating the exercises every day so that entire junit stuff can be covered to an extent.If you have clarifications on junit feel free to ping me

Thx
Karthik

Mohammad Abou-Basha said...

Thanks Maga, But how could I reach you?

arthik said...

good post