introduction to junit 3 8 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to JUnit 3.8 PowerPoint Presentation
Download Presentation
Introduction to JUnit 3.8

Loading in 2 Seconds...

play fullscreen
1 / 15

Introduction to JUnit 3.8 - PowerPoint PPT Presentation

  • Uploaded on

Introduction to JUnit 3.8. SEG 3203 Winter ‘07 Prepared By Samia Niamatullah. JUnit. JUnit is a simple, open source framework to write and run repeatable tests. JUnit features include: Assertions for testing expected results Test fixtures for sharing common test data

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Introduction to JUnit 3.8' - kirima

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to junit 3 8

Introduction to JUnit 3.8

SEG 3203

Winter ‘07

Prepared By

Samia Niamatullah

  • JUnit is a simple, open source framework to write and run repeatable tests. JUnit features include:
    • Assertions for testing expected results
    • Test fixtures for sharing common test data
    • Test runners for running tests
  • JUnit was originally written by Erich Gamma and Kent Beck.
  • Used for unit testing in Java. Other xUnit frameworks exist for other languages.
  • Supported by
earlier unit testing
Earlier Unit Testing
  • Use a debugger to go through code line by line.
    • Good to locate bugs.
    • Difficult for large system.
    • Impossible for regression testing.
  • Insert output statements in code
    • Easy to implement.
    • Rely on human eyes to recognize problems.
    • Hard to organize… causes the dreaded "Scroll Blindness".
unit testing
Unit Testing
  • Programmers have aversion towards writing testing code
    • Too busy.
    • It is difficult to write and maintain.
  •  Greater need to automate and simplify the process of writing unit tests
how can junit help
How Can JUnit help?
  • JUnit automates testing.
    • In fact, the organization and execution of testing.
  • JUnit saves 50% of your work, not all.
    • Still need to design test cases and implement it.
    • Fortunately, those are all needed to be done.
  • Write tests once, but run them as often needed.
  • Provides a well designed framework.
  • Abstracts common concerns of unit testing.
  • Easy to integrate test cases and test suites.
test method vs test case
Test Method vs. Test Case
  • Test Case
    • A test case is usually a single run of a specific functionality.
  • Test Method
    • A test method can contain one or more test cases.
    • A test method usually has one or more assert statements or fail statements.
  • Test Suit
    • A collection of of test cases/classes executed together
code under test
Code Under Test
  • Suppose we want to implement a basic calculator program.
  • It has the general math operations- add, subtract, multiply, divide and square root.
  • Methods take a single parameter n and perform the operation with the stored value result.

public class Calculator {

private int result;

public void add(int n) {

result = result + n;


public void substract(int n) {

result = result - n;


anatomy of the test class
import junit.framework.*;

public class CalculatorTest extends TestCase {

public CalculatorTest(String testName) {



protected void setUp() throws Exception {


protected void tearDown() throws Exception {


public void testAdd() {


int n = 0;

Calculator instance = new Calculator();


fail("The test case is a prototype.");



The test class should extend junit.framework.TestCase

Tests must be publicvoid

All test methods should have a name start with “test”

All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit.

If the method in the class under test is private, the test class must be in the same package.

Anatomy of the Test Class
anatomy of the test class1
Anatomy of the Test Class
  • import junit.framework.*;
    • The classes needed for using the assert methods.
  • public class CalculatorTest extends TestCase {
    • The use of the syntax prefixes can be avoided by extending the TestCase class.
  • setUp(), tearDown()
    • Run to do some common things that are done before and after each test
    • For example, in calculator example, we might like to reset the calculator to zero before and after testing each operation.
  • assert*() methods
    • Used to compare the obtained and expected results
    • The failure/success of the tests depend on their outcome.
test fixture
Test Fixture
  • Useful if you have two or more tests for a common set of objects.
  • Avoids duplicating the code necessary to initialize (and cleanup) the common objects.
  • Tests don't share the state of objects in the test fixture.
  • setUp() and tearDown() methods can be used to define fixtures
junit framework assert
  • Provide static methods which can help comparing the expected result and actual result.
  • If any assert is violated, a failure will be recorded.

assertEquals (expected, actual) assertEquals (message, expected, actual)

assertSame (expected, actual) assertSame (message, expected, actual)

assertNotSame (unexpected, actual) assertNotSame (message, unexpected, actual)

assertFalse (condition) assertFalse (message, condition)

assertTrue (condition) assertTrue (message, condition)

assertNotNull (object) assertNotNull (message, object)

assertNull (object) assertNull (message, object)

fail () fail (message)

test execution
  • Execute a test by using the Run function of the IDE.
  • NetBeans/Eclipse, can use a default test runner-- all the tests in the class run one by one.


  • Manually control the execution of the test program and select which test classes/methods to run by including the following method:

public static Test suite( ) {

TestSuite suite = new TestSuite( );

suite.addTest(new <class name>(“<method name>”) );


return suite;


status of a test
Status of a Test
  • A test is a single run of a test method.
  • Success
    • A test succeeds in time when No assert is violated; No fail statement is reached; No unexpected exception is thrown.
  • Failure
    • A test fails when an assert is violated or a fail statement is reached.
  • Error
    • An unexpected exception is thrown or timeout happens.
status of a test1
Status of a Test
  • On failure and error, the test results also show a stack trace of the execution.
  • LAB-5110  NetBeans™: JUnit (April 2005) (
  • Unit Testing in Eclipse Using JUnit by Laurie Williams, Dright Ho, and Sarah Smith (
  • JUnit Testing With Netbeans (
  • JUnit 4 Tutorial by Ji Chao Zhang, October 23, 2006 (CSI 5111 presentation) Based on “Get Acquainted with the New Advanced Features of JUnit 4” by Antonio Goncalves
  • JUnit Test Infected: Programmers Love Writing Tests; Kent Beck, Erich Gamma.
  • JUnit FAQ Edited by Mike Clark (