Eclipse Junit Testing Tutorial
Eclipse Junit Testing Tutorial
JUnit is a simple Java testing framework to write tests for you Java application. This
tutorial gives you an overview of the features of JUnit and shows a little example how
you can write tests for your Java application.
General
Author:
Sascha Wolski
Sebastian Hennebrueder
Date:
April, 12 2005
Software:
Eclipse 3.x
Junit 2.x
Source code:
http://www.laliluna.de/assets/tutorials/junit-testing-source.zip
PDF Version
http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf
What is JUnit
JUnit is a simple open source Java testing framework used to write and run repeatable
automated tests. It is an instance of the xUnit architecture for unit testing framework.
Eclipse supports creating test cases and running test suites, so it is easy to use for your
Java applications.
2. Write a test method to assert expected results on the object under test:
3. Write a suite() method that uses reflection to dynamically create a test suite
containing all the testXXX() methods:
public static Test suite(){
return new
TestSuite(BookTest.class);
}
You find the JUnit tab near the Package Explorer tab. You can change the position of the
tab by drag and drop it.
5. Right click on the subclass of TestCase and choose Run > JUnit Test to run the
test.
To create a test fixture, define a setUp() method that initializes common object and a
tearDown() method to cleanup those objects. The JUnit framework automatically invokes
the setUp() method before a each test is run and the tearDown() method after each test is
run.
In static way you override the runTest() method inherited form TestCase class and call
the desired test case. A convenient way to do this is with an anonymous inner class.
Note: Each test must be given a name, so you can identify it if it fails.
The dynamic way to create a test case to be run uses reflection to implement runTest. It
assumes the name of the test is the name of the test case method to invoke. It dynamically
finds and invokes the test method. The dynamic way is more compact to write but it is
less static type safe. An error in the name of the test case goes unnoticed until you run it
and get a NoSuchMethodException. We leave the choice of which to use up to you.
What is a TestSuite
If you have two tests and you'll run them together you could run the tests one at a time
yourself, but you would quickly grow tired of that. Instead, JUnit provides an object
TestSuite which runs any number of test cases together. The suite method is like a main
method that is specialized to run tests.
Create a suite and add each test case you want to execute:
Since JUnit 2.0 there is an even simpler way to create a test suite, which holds all
testXXX() methods. You only pass the class with the tests to a TestSuite and it extracts
the test methods automatically.
Note: If you use this way to create a TestSuite all test methods will be added. If you do
not want all test methods in the TestSuite use the normal way to create it.
Example:
A little example
Create a new Java project named JUnitExample.
Add a package de.laliluna.tutorial.junitexample where you place the example classes and
a package test.laliluna.tutorial.junitexample where you place your test classes.
Add two properties title of type String and price of type double.
Add a method trunk for a method equals(Object object) which checks if the object is an
instance of the class Book and the values of the object are equal. The method return a
boolean value.
Note: Do not write the logic of the equals(..) method, we do it after finish creating the test
method.
/**
* Constructor
*
* @param title
* @param price
*/
public Book(String title,
double price) {
this.title = title;
this.price = price;
}
/**
* Check if an object is an instance of book
* and the values of title and price are equal
* then return true, otherwise return false
*/
public boolean equals(Object object) {
return false;
}
public double getPrice() {
return price;
}
public void
setPrice(double price) {
this.price = price;
}
public void
setTitle(String title) {
this.title = title;
}
}
In the wizard choose the methods stubs setUp(), tearDown() and constructor().
/**
* setUp() method that initializes common objects
*/
protected void setUp() throws Exception {
super.setUp();
}
/**
* tearDown() method that cleanup the common objects
*/
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Constructor for BookTest.
* @param name
*/
public BookTest(String name) {
super(name);
}
Now we want to write a test for the equals(..) method of the class Book. We provide three
private properties, book1, book2 and book3 of type Book.
Within the setUp() method we initializes the three properties with some values. Property
book1 and book3 are the same.
Now, add a test method testEquals() to the test case. Within the method we use the
assertFalse() method of the JUnit framework to test if the return-value of the equals(..)
method is false, because book1 and book2 are not the same. If the return-value is false the
logic of the equals() method is correct, otherwise there is a logical problem while
comparing the objects. We want to test if the method compares the objects correctly by
using the assertTrue() method. Book1 and Book3 are the same, because both are an
instance of the class Book and have the same values.
Note: You can also create a separate class where you add the suite() method.
Within the method create a new instance of TestSuite and use the method addTest(..) to
add a test. Here we use the dynamically way to add a test to a TestSuite.