I'll be totally honest... I'm not really excited about the TDD hype. But I feel like it's something important and even when I feel lazy(what the hell is wrong with me!??) I managed to atleast give it a shot. I've been reading this morning about JUnit and what it's all about.
http://junit.sourceforge.net/doc/testinfected/testing.htmThe above URL gave me an introduction to JUnit. Eventhough most bloggers here already did that, I'd like to say thanks to the author because his article was number one in google when a keyed in "junit tutorial"
For those who are wanting to see JUnit in action, I don't think the article I mentioned was sufficient, you'll probably be needing this also...
http://open.ncsu.edu/se/tutorials/junit/which will give you an idea on how to use Eclipes' built-in support for JUnit.
Ok, let's get on it.
First on, I assume you already downloaded the latest version of JUnit. Unpack the zip file, and for your own convenience try putting the jar file in JDK_INSTALL/jre/lib/ext folder or put it in your CLASSPATH environment variable.
following the first article I gave you, type in the following code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package test;
public class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { this.fAmount = amount; this.fCurrency = currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; } public Money add(Money m) { return new Money( ( m.amount() + this.amount() ) , currency() ); }
} |
put this is a package named test. Now on the same package, create a class named MoneyTest. Note the following convention when making a unit test
classNameTest
methodNameTest
though I may defy them
for the sake of this example(I'm following two articles). Here's the code listing for MoneyTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package test;
import junit.framework.TestCase; import junit.framework.Assert;
public class MoneyTest extends TestCase {
public void testSimpleAdd() { Money m12CHF = new Money(12, "CHF"); Money m14CHF = new Money(14, "CHF"); Money expected = new Money(26, "CHF"); Money result = m12CHF.add(m14CHF); Assert.assertTrue( expected.equals(result) ); } } |
This test, as of this time doesn't do anything but to test if the two objects' values are equal. According to this
link, If it's not defined for a (user) class(the equals method is not overidden), it behaves the same as ==). And so, my test returned a failed result. Here's my screenshot for the simple test I made...

I wonder how I could use this with our current project. Problem is I am currently in a fast-phased project... There's not even a time for documentation(only code based). For those of you who are interested in using JUnit outside of Eclipse, I have researched a little for you. You can find the instructions in this link...
http://rollerjm.free.fr/pro/Junit.htmlWhen I get the time, I'll try to experiment more on this. As for now(YAWN)... I need to get some sleep... Hope that help. :)