junit & eclipse1 department of computer science and software engineering concordia university...

34
JUnit & Eclipse 1 JUnit & Eclipse JUnit & Eclipse DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey Paquet

Upload: diana-hall

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

JUnit & Eclipse 1

JUnit & EclipseJUnit & Eclipse

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY

Feb 2, 2009

revision 1.2 – Feb 2, 2009revision 1.2 – Feb 2, 2009

by Emil Vassev & Joey Paquet

JUnit & Eclipse 2

Outline

TestingTesting Unit TestingUnit Testing Unit Testing FrameworksUnit Testing Frameworks JUnit – the Java’s Unit Testing FrameworkJUnit – the Java’s Unit Testing Framework

IntroductionIntroduction BenefitsBenefits JUnit NotionsJUnit Notions Assertion Statement ReferenceAssertion Statement Reference ExampleExample

Unit Testing in Eclipse using JUnitUnit Testing in Eclipse using JUnit IntroductionIntroduction JUnit Test CasesJUnit Test Cases JUnit Test SuitsJUnit Test Suits

JUnit & Eclipse 3

Testing

Software testing is meant to avoid software failure. A failure is caused by a fault in the code base. A symptom is an observable behavior of the system that enables us to observe

a failure and possibly find its corresponding fault. The process of discovering what caused a failure is called fault identification. The process of ensuring that the failure does not happen again is called fault

correction, or fault removal. Fault identification and fault correction is popularly called debugging. Software testing, in practice, is about identifying a certain possible system

failure and design a test case that proves that this particular failure is not experienced by the software.

“testing can reveal only the presence of faults, never their absence.” [Dijkstra]

:: Definition

JUnit & Eclipse 4

Testing

There are many driving sources for software testing: Requirements-driven testing, Structure-driven testing, Statistics-driven testing,

Risk-driven testing.

There are many levels and kinds of software testing: Unit Testing, Integration Testing, Function Testing, Acceptance Testing,

Installation Testing.

At the day-to-day programming level, unit testing can easily be integrated in the programming effort by using a Unit Testing Framework.

However, unit testing cannot be applied for higher-level testing purposes such as function testing or acceptance testing, which are system-level testing activities.

:: Definition

JUnit & Eclipse 5

Unit Testing

Defintion: A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality applied to one of the units of the code being tested. Usually a unit test exercises some particular method in a particular context.

Example: add a large value to a sorted list, then confirm that this value appears at the end of the list.

The goal of unit testing is to isolate important parts of the program and show that the individual parts are free of certain faults.

:: Definition

JUnit & Eclipse 6

Unit Testing

Facilitates change: Unit testing allows the programmer to change or refactor code at a later date,

and make sure the module still works correctly (i.e. regression testing).

Simplifies integration: Unit testing helps to eliminate uncertainty in the units and can be used in a

bottom-up integration testing style approach.

Documentation: Unit testing provides a sort of living documentation of the specifications of the

units of the system. Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain understanding of the unit’s API specifications.

:: Benefits

JUnit & Eclipse 7

Unit Testing

Identifies defects early in the development cycle. Many small bugs ultimately leads to chaotic system behavior,

which becomes increasingly difficult to work on. Successful (and meaningful) tests breed confidence. Makes sure that further changes do not introduce problems into

previously correct code. Testing forces the programmers to read and analyze their code,

thus removing defects through constant code verification.

:: Benefits (cont.)

JUnit & Eclipse 8

Unit Testing Framework

For a large system, there can be thousands of unit tests, which can be tedious to maintain and execute.

Automated tests support maintainability and extensibility along with efficiency.

A xUnit Testing Framework lets a programmer associate Classes and Methods to corresponding Test Classes and Test Methods.

Automation is achieved by automatically setting up a testing context, calling each test case, verifying their corresponding expected result, and reporting the status of all tests.

Can be combined with the use of a Software Versioning Repository: prior to any commit being made, unit testing is re-applied to make sure that the committed code is still working properly.

:: Rationale

JUnit & Eclipse 9

JUnit – the Java’s xUnit Testing Framework

In Java, the standard unit testing framework is known as JUnit.

Test Cases and Test Results are Java objects.

JUnit was created by Erich Gamma and Kent Beck, two authors best known for Design Patterns and eXtreme Programming, respectively.

Using JUnit you can easily and incrementally build a test suite that will help you measure your progress, spot unintended side effects, and focus your development efforts.

:: Introduction

JUnit & Eclipse 10

JUnit – the Java’s Unit Testing Framework

Tested Class – the class that is being tested. Tested Method – the method that is tested. Test Case – the testing of a class’s method against some specified conditions.

Test Case Class – a class performing the test cases. Test Case Method – a Test Case Class’s method implementing a test case.

Test Suite – a collection of test cases that can be tested in a single batch.

:: Key JUnit Notions

JUnit & Eclipse 11

JUnit – the Java’s Unit Testing Framework

The class TestCase has four important methods – run(), setUp(), tearDown() and runTest().

TestCase.run() applies Template Method pattern

public void run(){

setUp();

runTest();

tearDown();

}

The Template Method pattern “defines the skeleton of an algorithm in an operation, deferring some steps to subclasses”.

All Test Case classes need to be subclasses to the TestCase class.

:: TestCase Class

JUnit & Eclipse 12

JUnit – the Java’s Unit Testing Framework

JUnit test runners automatically invoke the setUp() method before running each Test Class.

This method typically initializes fields, turns on logging, resets environment variables, and so forth, i.e. it sets up a context for the test cases to be applied.

protected void setUp()

{

System.out.println("Before testing");

}

In JUnit 4, the initialization method no longer needs to be called setUp(). It just needs to be denoted with the @Before annotation. We can have multiple methods noted @Before, each running before testing.

@Before protected void initialize()

{

System.out.println("Before testing");

}

:: TestCase Class – SetUp

JUnit & Eclipse 13

JUnit – the Java’s Unit Testing Framework

If we need at the end of each test to do a cleanup operation, we can use JUnit’s tearDown() method. For example we can call the garbage collector there in case our tests consume large amount of memory.

protected void tearDown()

{

System.out.println(“After testing");

System.gc();

}

In JUnit 4, we can give it a more natural name and annotate it with @After.

@After protected void disposeObjects ()

{

System.out.println(“After testing");

System.gc();

}

:: TestCase Class – TearDown

JUnit & Eclipse 14

JUnit – the Java’s Unit Testing Framework

1. Create the class that you want to test.

2. Build the test class - with the needed imports and extensions for JUnit. Extend this class from junit.framework.TestCase. Name all the test methods with a prefix of ‘test’.

3. Code the actual test cases. Validate conditions and invariants using one of the several assert methods.

import junit.framework.*;

public class TestFailure extends TestCase {

public void testSquareRootException() {

try {

SquareRoot.sqrt(-4, 1);

fail("Should raise an exception");

}

catch (Exception success) { … }

}

}

:: Basics - JUnit 3

Tested Class and Method

Test Case Method

Assertion Statement

Test Case Class

JUnit & Eclipse 15

JUnit – the Java’s Unit Testing Framework

Tests are identified by an @Test annotation and we no longer need to prefix our test methods with “test”.

This lets us follow the naming convention that best fits our application.

import junit.framework.*;

import org.junit.Test;

public class TestAddition extends TestCase {

private int x = 1;

private int y = 1;

@Test public void addition()

{

int z = x + y;

assertEquals(2, z);

}

}

:: Basics – JUnit 4

JUnit & Eclipse 16

JUnit – the Java’s Unit Testing Framework :: Example – Tested Class

JUnit & Eclipse 17

JUnit – the Java’s Unit Testing Framework :: Example – Test Class

JUnit & Eclipse 18

JUnit – the Java’s Unit Testing Framework

List of different types of assertion statements that you can use to test your code. These assertions are taken from the JUnit API.

assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) - used on doubles or

floats, where delta is the difference in precision. assertEquals(message, expected, actual, delta) - used on

doubles or floats, where delta is the difference in precision. assertFalse(condition) assertFalse(message, condition) assertNotNull(object) assertNotNull(message, object)

:: Assertion Statement Reference I

JUnit & Eclipse 19

JUnit – the Java’s Unit Testing Framework

assertNotSame(expected, actual) assertNotSame(message, expected, actual) assertNull(object) assertNull(message, object) assertSame(expected, actual) assertSame(message, expected, actual) assertTrue(condition) assertTrue(message, condition) fail() fail(message) failNotEquals(message, expected, actual) failNotSame(message, expected, actual) failSame(message)

:: Assertion Statement Reference II

JUnit & Eclipse 20

Unit Testing in Eclipse using JUnit

Eclipse comes with both JUnit and a plug-in for creating and working with JUnit tests.

Eclipse allows you to quickly create test case classes and test suite classes to write your test code in.

With Eclipse, Test Driven Development (TDD), becomes very easy to organize and implement.

Eclipse facilitates the testing by generating automatically stubs for testing class methods.

:: Introduction

JUnit & Eclipse 21

Unit Testing in Eclipse using JUnit

Once the class we want to test, is created we can start with building the test cases.

To create a test case do [File New JUnit Test Case]

:: Adding a Test Case to the Project

Put the test case class into the same package as the tested class.

JUnit & Eclipse 22

Unit Testing in Eclipse using JUnit :: Writing Test Cases

This is a test case class for testing SquareRoot class. The following test case methods test different aspects of the sqrt() method: The testSquareRootException() method

demonstrates how to test if an exception is properly thrown.

The testSquareRootPrecision() method tests against the precision of SquareRoot.sqrt().

The testSquareRootAlgorithm() method tests the square root algorithm.

JUnit & Eclipse 23

Unit Testing in Eclipse using JUnit :: Running the Test

From Menu bar [Run Run As JUnit Test],

or:

JUnit & Eclipse 24

Unit Testing in Eclipse using JUnit :: Test Result Analysis

These two test case methods

reported failures

JUnit & Eclipse 25

Unit Testing in Eclipse using JUnit :: Test Result Analysis

The following test fails because of insufficient precision:

Abs(Math.sqrt(67) - SquareRoot.sqrt(67, 5)) > 0.000001Abs(Math.sqrt(67) - SquareRoot.sqrt(67, 5)) > 0.000001

public void testSquareRootPrecision() throws Exception

{

Assert.assertEquals("SquareRoot precision less than 0.000001",

Math.sqrt(67), SquareRoot.sqrt(67, 5), 0.000001);

}

In order to increase the precision, we increase the number of iterations from 5 to 6, to arrive at: Assert.assertEquals("SquareRoot precision less than 0.000001",

Math.sqrt(67), SquareRoot.sqrt(67, 6), 0.000001);

JUnit & Eclipse 26

Unit Testing in Eclipse using JUnit :: Test Result Analysis

The following test fails because the SquareRoot.sqrt() method does not compute the exact square root of number 67.

public void testSquareRootAlgorithm() throws Exception

{

Assert.assertTrue(67 == (SquareRoot.sqrt(67, 6) * SquareRoot.sqrt(67, 6)));

}

In order to fix this problem we increase the number of iterations from 6 to 7, to arrive at:

Assert.assertTrue(67 == (SquareRoot.sqrt(67, 7) * SquareRoot.sqrt(67, 7)));

JUnit & Eclipse 27

Unit Testing in Eclipse using JUnit :: Rerunning the Test

All the test case methods passed

the test.

JUnit & Eclipse 28

Unit Testing in Eclipse using JUnit :: Test Suit - Introduction

We have performed tests on only one class, i.e. we have tested methods under the consideration they belong to the same class.In large projects we have many classes with methods that should be tested. For testing multiple classes Eclipse and JUnit expose the concept of Test Suit. A Test Suit is a collection of test cases that can be tested in a single batch. A Test Suite is a simple way of running one program that, in turn, runs all test cases.

JUnit & Eclipse 29

Unit Testing in Eclipse using JUnit :: Creating a Test Suit

There are four ways to create a JUnit Test Suite Class. First, select the directory (usually unittests/) that you wish to create the test suite class in.

Select [File New Other... Java JUnit JUnit Test Suite].

Select the arrow of the button in the upper left of the toolbar. Select [Other... Java JUnit JUnit Test Suite].

Right click on a package in the Package Explorer view in the Java Perspective, and select [Other... Java JUnit JUnit Test Suite].

You can create a normal Java class, but import the package junit.framework and extend the TestSuite class.

JUnit & Eclipse 30

Unit Testing in Eclipse using JUnit :: Adding a New Class to be Tested

This class is taken from the tutorial paper “JUnit Test Infected: Programmers Love Writing Tests”, by Kent Beck and Erich Gamma

JUnit & Eclipse 31

Unit Testing in Eclipse using JUnit :: Adding a New TestCase ClassThis class is taken from the tutorial paper “JUnit Test Infected: Programmers Love Writing Tests”, by Kent Beck and Erich Gamma.

JUnit & Eclipse 32

Unit Testing in Eclipse using JUnit :: Creating the Test Suite Class

Creating a Test Suit is straight forward and you just have to follow the wizard. All the tests would be taken care of by JUnit.

JUnit & Eclipse 33

Unit Testing in Eclipse using JUnit :: Running All Tests

Right click on the test suite class and select [Run As JUnit Test]

JUnit & Eclipse 34

JUnit JUnit FAQ JUnit API Eclipse An early look at JUnit 4 Pragmatic Unit Testing in Java with JUnit http://newton.cs.concordia.ca/~paquet/wiki/index.php/Testing http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks

Resources