Top Banner
36

Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Dec 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.
Page 2: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Lesson 7Unit Testing /JUnit/

AUBG ICoSCIS TeamAssoc. Prof. Stoyan Bonev

March, 23 - 24, 2013 SWU, Blagoevgrad

Page 3: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

3

Lesson Contents:

Unit Testing

Introduction to JUnit

JUnit within/under NetBeans

Page 4: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Unit Testing

4

Page 5: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Categories of Testing

5

User needs Acceptance testing

Requirementspecification

System testing

Design

Code

Integration testing

Unit testing

Page 6: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Testing 6

Unit Testing

Different modules tested separatelyFocus: to detect defects injected during

codingUT is closely associated with codingFrequently the developer himself does UT SDM coding phase sometimes called

“coding and unit testing” phase

Page 7: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

What is Unit Testing?

A Unit Test is a procedure to validate a single Functionality of an application.

One Functionality One Unit Test Unit Tests are automated and self-checked. They run in isolation of each other. They do NOT depend on or connect to

external resources, like DB, Network etc. They can run in any order and even parallel

to each other.

7Impetus Confidential

Page 8: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

JUnit– An Introduction -

8

Page 9: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

JUnit – An introduction JUnit is a unit testing framework for

Java. It comes from the family of unit testing

frameworks, collectively called xUnit, where ‘x’ stands for the programming language, e.g. NUnit for C#, CPPUnit, JSUnit, PHPUnit, PyUnit, RUnit etc. Kent Beck & Erich Gamma originally wrote this framework for ‘smalltalk’, called SUnit.

9Impetus Confidential

Page 10: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Why choose JUnit?

JUnit is simple and elegant. JUnit checks its own results and provide

immediate customized feedback. JUnit is hierarchial. JUnit has the widest IDE support, Eclipse,

NetBeans, BlueJ, …. JUnit is recognized by popular build tools

like, ant, maven etc. And… it’s free.

10Impetus Confidential

Page 11: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Design of JUnit

11Impetus Confidential

junit.framework.TestCase is the abstract command class which you subclass into your Test Classes.

junit.framework.TestSuite is a composite of other tests, either TestCase or TestSuite. This behavior helps you create hierarchal tests with depth control.

Page 12: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Design of JUnit

12Impetus Confidential

Image Courtesy: JUnit: A Cook’s Tour

Page 13: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Write a test case Define a subclass XXXTest of junit.framework.TestCase

public class CalculatorTest extends TestCase {

}

Define one or more testXXX() methods that can perform the tests and assert expected results. public void testAddition(){

Calculator calc = new Calculator();

int expected = 25;

int result = calc.add(10, 15);

assertEquals(result, expected); // asserting

}13

Impetus Confidential

Page 14: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Write a test case Each test method has to be structured like this:

public void testAddition(){

// declarations

Calculator calc = new Calculator();

// preparing input /expected/ data

int expected = 25;

// computing result /real, actual/ data

int result = calc.add(10, 15);

// comparing result data to expected data

assertEquals(result, expected); // asserting

}14

Impetus Confidential

Page 15: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Write a test case Override setUp() method to perform initialization.

@Override

protected void setUp() {

// Initialize anything, like

calc = new Calculator();

}

Override tearDown() method to clean up.@Override

protected void tearDown() {

// Clean up, like

calc = null; // gc, no need of delete operator

}15

Impetus Confidential

Page 16: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Asserting expectations assertEquals (expected, actual) assertEquals (message, expected, actual) assertEquals (expected, actual, delta) assertEquals (message, expected, actual, delta) assertFalse ((message)condition) Assert(Not)Null (object) Assert(Not)Null (message, object) Assert(Not)Same (expected, actual) Assert(Not)Same (message, expected, actual) assertTrue ((message), condition)

16Impetus Confidential

Page 17: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Failure?

JUnit uses the term failure for a test that fails expectedly.That is An assertion was not valid or

A fail() was encountered.

17Impetus Confidential

Page 18: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Write a test casepublic class CalculatorTest extends TestCase {

// initialize

protected void setUp()... S

public void testAddition()... T1

public void testSubtraction()... T2

public void testMultiplication()...T3

// clean up

protected void tearDownUp()... D

}

Execution will be S T1 D, S T2 D, S T3 D in any order. 18

Impetus Confidential

Page 19: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Write a test suite

Write a class with a static method suite() that creates a junit.framework.TestSuite containing all the Tests.

public class AllTests {

public static Test suite() {

TestSuite suite = new TestSuite();

suite.addTestSuite(<test-1>.class);

suite.addTestSuite(<test-2>.class);

return suite;

}

}

19Impetus Confidential

Page 20: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Run your tests

You can either run TestCase or TestSuite instances.

A TestSuite will automatically run all its registered TestCase instances.

All public testXXX() methods of a TestCase will be executed. But there is no guarantee of the order.

20Impetus Confidential

Page 21: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Run your tests JUnit comes with TestRunners that run your tests and

immediately provide you with feedbacks, errors, and status of completion.

JUnit has Textual and Graphical test runners.

Textual Runner >> java junit.textui.TestRunner AllTests

or,

junit.textui.TestRunner.run(AllTests.class);

Graphical Runner >> java junit.swingui.TestRunner AllTests

or,

junit.swingui.TestRunner.run(AllTests.class);

IDE like Eclipse, NetBeans “Run as JUnit” 21Impetus Confidential

Page 22: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

Mocks

Mocking is a way to deal with third-party dependencies inside TestCase.

Mocks create FAKE objects to mock a contract behavior.

Mocks help make tests more unitary.

22Impetus Confidential

Page 23: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

How to approach?

23Impetus Confidential

Test a little, Code a little, Test a little, Code a little … doesn’t it rhyme? ;)

Write tests to validate functionality, not functions.

If tempted to write System.out.println() to debug something, better write a test for it.

If a bug is found, write a test to expose the bug.

Page 24: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

JUnit– within/under

NetBeans -

.24

Impetus Confidential

Page 25: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

25

JUnit within/under NetBeans

Practical Manual:

Practical conventions

How to build unit tests?

How to run unit tests?Demo taskPractical exercise task

Page 26: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

26

Practical conventions

Each production code class X has a test code class Xtest derived from TestCase class

Each method y(){…} from class X has a method testy(){…} from Xtest class

Each test code class Xtest has setUp() method and tearDown() method

Page 27: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

27

Practical conventions

All production code located in a separate project folder Source Packages

All test code located in another separate project folder Test Packages

The skeletal structure and mostly the contents of a test code class Xtest and test code methods generated automatically

Page 28: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

28

How to build unit tests?Part 1Create New projectAdd Java class with main() methodAdd user defined class to be tested

as part of file with Java main() class or as a separate file within the same package.

Build the project and run it as a regular Java application

Run > Run Main project (F6)

Page 29: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

29

How to run unit tests?Part 2aSelect Java class to be tested (from Projects…)Click the right mouse button and invoke

Tools > Create TestsModify the generated text if necessary

Re-Build the project and run it as a JUnit testRun > Test Project(…) (Alt+F6)

Analyze the test results

Page 30: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

30

How to run unit tests?

OR(follow option 2b)

Page 31: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

31

How to run unit tests?Part 2bAdd a new File to the project

File > new File…Select category: Unit TestSelect File Type: Test for Existing ClassYou have to specify the class to be testedYou have to select JUnit version: 3The test class automatically generatedModify the generated text if necessary

Re-Build the project and run it as a JUnit testRun > Test Project(…) (Alt+F6)

Analyze the test results

Page 32: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

32

Demo Task

NetBeans project: SBJUnitTestClassLibrary

Page 33: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

33

Demo Task

NetBeans project: SBJUnitDistance01

Page 34: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

34

Practical Exercise TaskExpand project SBJUnitTestClassLibrary

Develop a class Power to implement arithmetic power operator with double base and integer exponent in two versions – iterative and recursive

Iterative method: double powi(double base, int n){ }

Recursive method: double powr(double base, int n){ }

Run the powx methods regular way Run the powx methods using Junit

• Create PowerTest class • Create testpowi() method and testpowr() method• Run JUnit

Page 35: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

35

Practical Exercise TaskExpand project SBJUnitDistance01

Develop a class Counter to implement a counter as a general purpose programming component with a count data item and methods void incCount() and method void decCount()

Run the Counter class regular way Run the Counter class methods using Junit

• Create CounterTest class • Create testincCount() method and testdecCount()

method• Run JUnit

Page 36: Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

36

Questions? And/Or

Thank You For

Your Attention!