My first attempt with JUnit

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

The 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.html

When 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. :)

Published 09-27-2006 10:14 AM by lamia
Filed under:

Comments

Wednesday, September 27, 2006 11:08 AM by jop

# re: My first attempt with JUnit

Frankly, I never encountered a project that was not fast-phased :-).

Anyways, if you really want to go fast, you should consider learning TDD. Whether it would help you in your current project, it is up to you and how fast you learn. I would not  typically suggest someone to try out anything, whether it is a new programming technique or a new tool, for the first time on a live project. "Never practice during the concert.", I read someone say, and I believe that to be true. If you want to dig TDD, practice first on some toy project. Unless, of course, you've got a someone there more experienced coaching you and pair-programming with you while doing TDD. That's is the best way of learning TDD imho. /jop

Wednesday, September 27, 2006 11:31 PM by bonskijr

# re: My first attempt with JUnit

.NET attributes is really what makes TDD easier to implement in the .NET platform, my observation with others (i tried dUnit of delphi win32) is that you have to inherit from a TestCase class since they don't have metadata support(aka attributes)

Thursday, September 28, 2006 9:14 PM by jokiz

# re: My first attempt with JUnit

yep, and the current JUnit uses annotations just like .NET

Saturday, September 30, 2006 6:51 AM by lamia

# re: My first attempt with JUnit

Nice suggestions! :) Thanks!