Cool Penguin

Simple developer guides

Java

More

on CoolPenguin.net:

Write a JUnit testcase in Java

JUnit is a popular testing framework. This guide shows how to write a JUnit test.

A class to be tested:

class Calculator {
    public int add(int i, int j) {
        return i + j;
    }
}
	

Rules for writing a JUnit testcase:

Our testcase:

import junit.framework.TestCase;

class CalculatorTestCase extends TestCase {
    public void testAdd() {
        try {
            Calculator calc = new Calculator();
            int result = calc.add(1, 2);
            assertEquals(result, 3);
        } catch(Exception e) {
            fail();
        }
    }
}
1 comments:
Mahender - 2009-11-18 00:16:21
simple and nice!
I think, example used here is too simple ....
Your name

Your comment