JUnit4 - First Steps
(Free Web Tutorial)
by Michael Thomas
JUnit Home Page
In this tutorial you will install JUnit, create your first Java Project
using JUnit and discuss TDD.
Note: On the JUnit & TDD Home page, you can download
this whole tutorial (all content, tutorials & examples) !!!
This tutorial covers:
Version: JUnit 4.9b2
Install file: <I used the one built into Eclipse> or junit4.9b2.zip, 1.6MB, January 18, 2011
Prerequisites
- Have JDK 6 or high installed. (May work with JDK 5)
- Eclipse (IDE-Integrated Development Environment)
- This tutorial has been tested in the following environment:
Date: 07/06/2011;
OS: Win 7;
JDK: 1.6.0_24
Eclipse: Eclipse Helios (IDE for Java EE Developers)
(1.3.0) (as of 08/11/2010)
Objectives
- Install JUnit
- Create your first Java Project using JUnit
- Write JUnit test cases using assertions
- Learn how to use TDD (Test Driven Development) for all your Java
projects.
Terms
FYI - Possible Errors:
JUnit Install
- Prerequisites - have JDK & Eclipse installed.
- Download JUnit - if needed.
- Note: Eclipse should have JUnit installed!
- Continue with tutorials without downloading unless you find you need
it.
Skip the download!
- http://junit.org
- Double Click the "Download JUnit" tab at the top to go to the
main download page.
- Right click on "Basic jar" file (ex: junit-4.9b2.jar)
and choose "Save target as". Warning: Your browser may change
the extension to .zip. If so change back to .jar.
- Notes on the files:
- junit-4.9b2.jar - Basic jar 240kb - this is the one you
want to use in your projects!
- junit4.9b2.zip - Source zip 1.6meg - this has the
source and documentation (javadocs, examples and other interesting
info).
- junit-4.9b2-src.jar 133kb - Source jar - has the source code.
Create a Test Java Project & Java Class
- Create a test Java Project
- File, New, Project
- Click "Java Project", then Next
- Project name: MyJUnit4FirstSteps
- Add the JUnit4 library.
- Right click on the Java Project, Properties, Java Build Path, click
the "Libraries" tab.
- Click "Add Library", JUnit, Next, then choose JUnit4 for the
"JUnit Library Version".
- Click "Finish", "Ok".
- Create a new source folder to place JUnit test class files in.
- File, New, Source Folder: test
- Create a Package in the "test" source folder.
- Right click on "src", New, Package: junit.learnnow
- Note: This will allow you to keep your test code in a
separate directory (source folder) but have access to package-private
data members (methods & variables) in the classes we will place in the
same package in the "src" source folder.
- Create a "JUnit Test Case" class
- Right click on the "test" package name "junit.learnnow"
and choose "JUnit Test Case"
- Name: MyClassTest
(Notice: Always use the <class name> followed by "Test" for the test classes
name)
We will later create the MyClass class.
- Note: If the "MyClass" already you can have the wizard
generate the JUnit test class.
- Class Under Test: MyClass
- Click "Next", and check the class & methods you want to test.
- Click "Finish"
- If you did not add the JUnit Library earlier you might get the prompt "JUnit 4 is not on the build path. Do you want to add it?"
click "OK".
- Put the following code in the class.
MyClassTest.java.v01.txt - click to
view the source code.
- Teaching Point!!! - Normally In TDD (Test Driven Development) you create the JUnit
Test Case Class ("MyClassTest") prior to creating the implementing Class ( "MyClass").
Then in small iterative cycles you add test methods to your JUnit test class
see it fail, implement fake it code to pass, then fix the code and see it
pass.
- Notice the following:
- Imports - 2 imports
import
static
org.junit.Assert.*;
import
org.junit.Test;
- Class Name - always follows the naming convention: <ClassName>Test
ie: MyClassTest
- Java Notation - using a Java notation of @Test before all test methods.
- Test Methods - all methods that you want JUnit to run will follow these
rules:
- Visibility is: public
- Return type is: void
- Arguments: None!
- Method names start with lower case "test"
- Note: The order of running the methods can NOT be predicted! Do
not make test cases that rely on other methods running first!
- Run the JUnit Test
- Click on the "MyClassTest" tab in the editor.
- Run the JUnit test. Click "Run", "Run As", "JUnit Test". Notice
the red bar because we don't have the code written yet.
(Note: You should be able to just click the green run Icon and it will run
the JUnit)
- Development Process is next....
- Normally at this point you would fill out the JUnit test code and create
the methods in the matching Class that you are testing as you go along.
In small increments you would code and test.
- For the sake of this tutorial let's just finish the JUnit test code.
- Put the following code in the class.
MyClassTest.java.txt - click to view the
source code.
- In this example, we later decided we needed the method "public void
testDisplayFile()" after the initial creation of the JUnit test case.
- Run the JUnit Test again.
- Create a Package in the "src" source folder.
- Right click on "src", New, Package: junit.learnnow
- Create a class
- Right click on the "src" package name "junit.learnnow"
and choose "JUnit Test Case"
- Name: MyClass
- Note: Remember at this point you normally will create code that makes a
test method turn green and repeat that process.
For the sake of this tutorial we will just finish out the source code.
- Put the following code in the JUnit class.
MyClass.java.txt - click to view the source
code.
- Run the JUnit Test
- Click on the "MyClassTest" tab in the editor.
- Run the JUnit test. Click "Run", "Run As", "JUnit Test". Notice
the green bar because we have implemented code that returns exactly what the
JUnit asserts expect.
(Note: You should be able to just click the green run Icon and it will run
the JUnit)
General JUnit Test Case Info
- Red bar - tests fail.
- Green bar - all tests pass.
Advanced Topics:
- How to call a private method from JUnit
- Sometimes you may feel the need to test a private method.
There is a way if the JVM has not been configured to disallow that.
- Example code :
(For a complete example, look at the code in the links above to "MyClassTest.java"
and "MyClass.java".
Check out the methods with the words "PrivateMethod" in them.)
Example Class method code:
private String examplePrivateMethod3 ( String myString1, String
myString2 ) {
return myString1 + myString2;
}
Example JUnit4 code:
public void testPrivateMethod3() {
MyClass myClass = new MyClass();
try {
//Test calling a private method with a 2 String parms.
java.lang.reflect.Method method =
MyClass.class.getDeclaredMethod(
"examplePrivateMethod3", String.class,
String.class); //NOTE this 1 of 2 changes!
method.setAccessible(true);
//method.invoke() returns an Object!
String result = (String) method.invoke(myClass, "Hello ",
"World"); //NOTE this 2 of 2 changes!
assertEquals("Hello World", result);
} catch (Exception e) {
fail("Ooops... developer's code is bad.");
}