JUnit 4 Tutorial. Xavier Noumbissi University of Waterloo

Similar documents
+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

Unit Testing JUnit and Clover

Jiří Tomeš. Nástroje pro vývoj a monitorování SW (NSWI026)

CPSC 330 Software Engineering

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

Tutorial 5: Developing Java applications

Test Automation Integration with Test Management QAComplete

JUnit Automated Software Testing Framework. Jeff Offutt. SWE 437 George Mason University Thanks in part to Aynur Abdurazik. What is JUnit?

Java Unit testing with JUnit 4.x in Eclipse

The junit Unit Tes(ng Tool for Java

A Short Introduction to Writing Java Code. Zoltán Majó

The "Eclipse Classic" version is recommended. Otherwise, a Java or RCP version of Eclipse is recommended.

Software Quality Exercise 2

Builder User Guide. Version Visual Rules Suite - Builder. Bosch Software Innovations

Unit Testing. and. JUnit

Building OWASP ZAP Using Eclipse IDE

Maven or how to automate java builds, tests and version management with open source tools

TeamCity A Professional Solution for Delivering Quality Software, on Time

Integration Knowledge Kit Developer Journal

Click Start > Control Panel > System icon to open System Properties dialog box. Click Advanced > Environment Variables.

MarathonITE. GUI Testing for Java/Swing Applications

JUnit. Introduction to Unit Testing in Java

Android: Setup Hello, World: Android Edition. due by noon ET on Wed 2/22. Ingredients.

Unit Testing and JUnit

Unit Testing & JUnit

Testing, Debugging, and Verification

Agile.NET Development Test-driven Development using NUnit

IBM Tivoli Workload Scheduler Integration Workbench V8.6.: How to customize your automation environment by creating a custom Job Type plug-in

APPENDIX A: MOCKITO UNIT TESTING TUTORIAL

Build management & Continuous integration. with Maven & Hudson

Builder User Guide. Version 5.4. Visual Rules Suite - Builder. Bosch Software Innovations

Going Interactive: Combining Ad-Hoc and Regression Testing

Escaping the Works-On-My-Machine badge Continuous Integration with PDE Build and Git

Software project management. and. Maven

Sonatype CLM for Maven. Sonatype CLM for Maven

GridWorld Installation Guide

TESTING WITH JUNIT. Lab 3 : Testing

Introduction to Eclipse

How They Do It in Switzerland Outsource the Code, Insource the Quality

An Introduction to Software Development Process and Collaborative Work

Installing (1.8.7) 9/2/ Installing jgrasp

Eclipse installation, configuration and operation

JUnit Howto. Blaine Simpson

RecoveryVault Express Client User Manual

IDE s for Java, C, C++ David Rey - DREAM

Quality Cruising. Making Java Work for Erlang. Erik (Happi) Stenman

The goal with this tutorial is to show how to implement and use the Selenium testing framework.

Attix5 Pro Server Edition

Effective unit testing with JUnit

Developing In Eclipse, with ADT

MultiAlign Software. Windows GUI. Console Application. MultiAlign Software Website. Test Data

LECTURES NOTES Organisational Aspects of Software Development

Online Backup Linux Client User Manual

LAB 6: Code Generation with Visual Paradigm for UML and JDBC Integration

Online Backup Client User Manual

How to use the Eclipse IDE for Java Application Development

Introduction to Synoptic

Sonatype CLM Enforcement Points - Continuous Integration (CI) Sonatype CLM Enforcement Points - Continuous Integration (CI)

TIBCO ActiveMatrix BusinessWorks Plug-in for TIBCO Managed File Transfer Software Installation

Configuration Manual Yahoo Cloud System Benchmark (YCSB) 24-Mar-14 SEECS-NUST Faria Mehak

Java Language Tools COPYRIGHTED MATERIAL. Part 1. In this part...

Team Name : PRX Team Members : Liang Yu, Parvathy Unnikrishnan Nair, Reto Kleeb, Xinyi Wang

Install guide for Websphere 7.0

IDS 561 Big data analytics Assignment 1

Attix5 Pro Server Edition

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

Robotium Automated Testing for Android

Code Estimation Tools Directions for a Services Engagement

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

INSTALLING MALTED 3.0 IN LINUX MALTED: INSTALLING THE SYSTEM IN LINUX. Installing Malted 3.0 in LINUX

Selenium An Effective Weapon In The Open Source Armory

Java with Eclipse: Setup & Getting Started

Introduction to Android Programming (CS5248 Fall 2015)

Selenium Automation set up with TestNG and Eclipse- A Beginners Guide

Principles of integrated software development environments. Learning Objectives. Context: Software Process (e.g. USDP or RUP)

1. Product Information

Hadoop Tutorial. General Instructions

Topics in Unit Testing Tools

Integration of. ERP systems and epages 6. with Web services

PDQ-Wizard Prototype 1.0 Installation Guide

Online Backup Client User Manual Linux

Content. Development Tools 2(63)

Automation using Selenium

Java. Java. e=mc 2. composition

Rational Application Developer Performance Tips Introduction

Installing Java. Table of contents

Writing Self-testing Java Classes with SelfTest

Topics in Unit Testing Tools

Tracking Network Changes Using Change Audit

Continuous integration with Jenkins CI

Java Programming Unit 1. Your first Java Program Eclipse IDE

ADT Plugin for Eclipse

Lazy OpenCV installation and use with Visual Studio

Tutorial: setting up a web application

Android Environment SDK

ERIKA Enterprise pre-built Virtual Machine

S. Bouzefrane. How to set up the Java Card development environment under Windows? Samia Bouzefrane.

Creating A Walking Skeleton

Creating a Free, Dependable Software Engineering Environment for Building Java Applications

Transcription:

JUnit 4 Tutorial Xavier Noumbissi xnoumbis@engmail.uwaterloo.ca University of Waterloo 1 Introduction The goal of this tutorial is to give an introduction to the JUnit [5, 1] framework that enables the reader to: Have a basic understanding of what JUnit is Implement test classes in JUnit 4 Get coverage information from tests execution (with Cobertura [3]) Test exceptions using JUnit JUnit is a framework that helps in the automation of unit tests for Java classes. It was created by Kent Beck and Erich Gamma. Similar tools have also been created for other languages. For example NUnit for the.net framework [6], CppUnit for C++ [4], and PyUnit for Python [7]. Version 4 is the latest release of JUnit (as of writing). It is backward compatible with the previous version JUnit 3.8. Here, we will only present the new conventions usage of JUnit 4. This new version was mainly released to take advantages of the annotations capabilities (JSR 175 [2]) introduced with Java 5.0. JUnit plugins are available for several IDE like Eclipse, Netbeans, etc. In this tutorial, we will not use any IDE for the demonstrations. We will run and collect test results from the command line. 1

Junit 4 Tutorial 2 2 Steps involved in performing unit tests (i) Prepare (or set up) the test environment to fulfill conditions that must be met, according to the test plan. This means to define and set prefix values. E.g. initialize fields, turn on logging, etc. (ii) Execute the test case. This means, executing (exercising) the part of the code to be tested. For that we use some test inputs (test case values), according to the test plan. (iii) Evaluate the results or side effects generated by the execution of the test case to check if they match what has been defined in the test plan. (iv) Clean up (or tear down) the test environment if needed so that further testing activities can be done, without being influenced by the previous test cases. We deal here with postfix values. 3 Realization of the unit testing steps in JUnit 4 Here we describe how the steps given in section 2 are to be implemented in the JUnit framework. The different steps are implemented in a Java test class. The methods of the test class receive specific annotations to make them known to the framework as such: (i) Prepare (or set up) the test environment: the different actions to realize this are to be implemented in one or several methods, annotated with @Before. Those methods are then executed before each test case (test method). @Before public void setup ( ) { s = new Sample ( ) ; (ii) Execute the test case and evaluate the results (or side effects): This is done by exercising the code under test with test values within a (test) method. Each method annotated with @Test will be executed as a test case by JUnit. Evaluation of the expected results (or side effects) is done in the same method using assertions. @Test public void t e s t A d d i t i o n ( ) { int a = 3, b = 6 ; int expectedoutput = ( a+b ) ; int r e s = s. Addition ( a, b ) ; a s s e r t E q u a l s ( expectedoutput, r e s ) ;

Junit 4 Tutorial 3 (iii) Clean up (or tear down) the test environment is done in one or several methods, that are run after execution of each test method. To belong to this group, a method has to be annotated with @After. @After public void teardown ( ) { s = null ; 4 Running test cases and collecting the results The JUnit 4 framework provides the class org.junit.runner.junitcore (a so called Test Runner) in order to run the tests and collect their results. The tests can be run from a Java program using the method org.junit.runner.junitcore.runclasses(). The tests can also be run from the command line with: java org.junit.runner.junitcore TestSample The results are printed to the console: JUnit version 4.8.1...I Time: 0.006 OK (4 tests) For a failing test, the following is printed: JUnit version 4.8.1..E..I Time: 0.011 There was 1 failure: 1) testprintaddition(testsample) org.junit.comparisonfailure: expected:<[8] > but was:<[9] > at org.junit.assert.assertequals(assert.java:123)... at TestSample.testPrintAddition(TestSample.java:113)... at org.junit.runner.junitcore.main(junitcore.java:45) FAILURES!!! Tests run: 4, Failures: 1

Junit 4 Tutorial 4 When using a plugin from within an IDE (such as Eclipse) results are generally printed on a GUI. 5 Getting coverage information Figure 1: Coverage Tool in the Unit Testing process Generally, one may want to gain information about parts of the Unit Under Test that was exercised. For that you may want to use a tool to get coverage information. Figure 1 illustrates this process. Coverage tools instrument (add instructions to the unit under test to gather coverage information) your code before you execute the test cases. Then, when the test cases are run, information about what part of the code was executed is written to a file (created by the tool). Then you can call the coverage tool to visualize parts of the code that were executed. These tools usually have a graphic user interface. In the following we present the coverage tool Cobertura. 5.1 Cobertura Cobertura is a coverage tool for Java. Here are the features from Cobertura from the tool website ( cobertura.sourceforge.net ) : Can be executed from ant or from the command line Instruments Java bytecode after it has been compiled

Junit 4 Tutorial 5 Can generate reports in HTML or XML Shows the percentage of lines and branches covered for each class, each package, and for the overall project Shows the McCabe cyclomatic code complexity of each class, and the average cyclomatic code complexity for each package, and for the overall product Can sort HTML results by class name, percent of lines covered, percent of branches covered, etc. It can sort in ascending or decending order Steps in using Cobertura with JUnit from the command line: (i) Compile your java classes: javac Sample.java TestSample.java (ii) Instrument the class files of the code you want to gather coverage information: cobertura-instrument.sh Sample.class (iii) Execute your unit tests: java org.junit.runner.junitcore TestSample (iv) Generate coverage information: cobertura-report.sh --destination <report directory> This action generates HTML files in the folder <report directory>, that contains tests coverage information. 5.2 Installing Cobertura To use Cobertura, download its latest distribution file from cobertura.sourceforge.net. After unpacking the archive, the location of the folder has to be included in the executable path. For example on Linux based machines using a bourne shell: export PATH=<Cobertura directory>:$path For how to use other features and commands of Cobertura, please refer to the tool website. 6 Testing Exception For testing Exceptions thrown by a method, we present here two methods. The first one was created with JUnit 4. Using it requires use of the annotation @Test with the

Junit 4 Tutorial 6 attribute expected: @Test ( expected=arithmeticexception. class ) public void testdividebyzero ( ) { s. dividebyzero ( ) ; However, if a developer wants to access details information about the exception instance, (s)he should rather use the older method from JUnit 3. In that case the method org.junit.assert.fail() can be used to trigger failure of a test. This can be desirable in cases where the expected exception was not thrown. @Test public void testanotherexception ( ) { try{ s. anotherexception ( 0 ) ; //With the method f a i l, we can s i g n a l i z e to // the framwork t h a t the t e s t f a i l e d. f a i l ( MyException was not thrown! ) ; catch ( MyException e ) { //Here we have a c c e s s to more i n f o a s s e r t N o t N u l l ( e. getmessage ( ) ) ; 7 Ignoring a Test JUnit gives also the possibility not to execute a test method. For that, the method has to be annotated with @Ignore. This may be desirable for instance in cases where a test method is incomplete. @Ignore ( Not Ready, to be changed ) @Test public void donothing ( ) { 8 Building Test Suites To group test cases into test suites, a suite class has to be defined. The annotation @Suite.SuiteClasses is used to specify the test classes to be incorporated to the suite.

Junit 4 Tutorial 7 import org. j u n i t. runner. RunWith ; import org. j u n i t. runners. S u i t e ; @RunWith( S u i t e. class ) @Suite. S u i t e C l a s s e s ({ TestSample. class, TestOther. class ) public class MySuite { 9 Installing JUnit For running JUnit, you need to download the latest jar file from junit.org and include it in your classpath. As of of writing, this file is junit-4-8-1.jar. JUnit can also be installed from most Linux distributions package manager. For instance, on Debian based distributions you can use the command: apt-get install junit4 to get JUnit 4. You should always be careful which version the package manager is actually installing. Using for example junit instead of junit4 with Debian (squeeze) will install version 3 of the JUnit framework. References [1] Elliotte Rusty Harold. An early look at junit4. http://www.ibm.com/developerworks/java/library/j-junit4.html, call on Jan. 14 2010. [2] Java Community Process. Java community process website. http://www.jcp.org/en/jsr/detail?id=175, call on Jan. 14 2010. [3] Cobertura team. Cobertura website. http://cobertura.sourceforge.net, call on Jan. 14 2010. [4] CppUnit team. Cppunit website. http://cppunit.sourceforge.net, call on Jan. 14 2010. [5] JUnit team. Junit website. http://www.junit.org, call on Jan. 14 2010. [6] Nunit team. Nunit website. http://www.nunit.org, call on Jan. 14 2010. [7] PyUnit team. Pyunit website. http://pyunit.sourceforge.net, call on Jan. 14 2010.