15
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah

Introduction to JUnit 3.8

  • Upload
    kirima

  • View
    67

  • Download
    1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to JUnit 3.8

Introduction to JUnit 3.8SEG 3203 Winter ‘07

Prepared BySamia Niamatullah

Page 2: Introduction to JUnit 3.8

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 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 www.junit.org

Page 3: Introduction to JUnit 3.8

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".

Page 4: Introduction to JUnit 3.8

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

Page 5: Introduction to JUnit 3.8

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.

Page 6: Introduction to JUnit 3.8

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

Page 7: Introduction to JUnit 3.8

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;

}

Page 8: Introduction to JUnit 3.8

Anatomy of the Test Class

import junit.framework.*;public class CalculatorTest extends TestCase { public CalculatorTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { }

public void testAdd() { System.out.println("add"); int n = 0; Calculator instance = new Calculator(); instance.add(n); fail("The test case is a prototype."); }

}

The test class should extend junit.framework.TestCase

Tests must be public void

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.

Page 9: Introduction to JUnit 3.8

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.

Page 10: Introduction to JUnit 3.8

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

Page 11: Introduction to JUnit 3.8

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)

Page 12: Introduction to JUnit 3.8

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.

Alternatively, 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;

}

Page 13: Introduction to JUnit 3.8

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.

Page 14: Introduction to JUnit 3.8

Status of a Test On failure and error, the test results also show a stack trace of the execution.

Page 15: Introduction to JUnit 3.8

References

LAB-5110  NetBeans™: JUnit (April 2005) (http://developers.sun.com/events/techdays/self_paced_labs.jsp)

Unit Testing in Eclipse Using JUnit by Laurie Williams, Dright Ho, and Sarah Smith (http://open.ncsu.edu/se/tutorials/junit/#section1_0)

JUnit Testing With Netbeans (http://www.fsl.cs.sunysb.edu/~dquigley/cse219/index.php?it=netbeans&tt=junit&pf=y)

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 (http://junit.sourceforge.net/doc/faq/faq.htm#overview_1)