JUnit3 - First Steps
(Free Web Tutorial)
(NOTE: JUnit4 has replaced
JUnit3)
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 3
Install file: <I used the one build into Eclipse>
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
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.
- Try to locate the older version of junit3.
- Download the .jar file. (Note: The .zip or -src.jar file
is not needed.)
Create a Test Java Project & Java Class
- Create a test Java Project
- File, New, Project
- Click "Java Project", then Next
- Project name: MyJUnit3FirstSteps
- Add the JUnit3 library.
- Right click on the Java Project, Properties, Java Build Path, click
the "Libraries" tab.
- Click "Add Library", JUnit, Next, then choose JUnit3 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 prompted "JUnit 3 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 - 1 import
import
junit.framework.TestCase;
- The class must extend TestCase:
extends TestCase
- Class Name - always follows the naming convention: <ClassName>Test
ie: MyClassTest
- 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.