Introduction to unit testing with Java, Eclipse and Subversion



Similar documents
Beginning with SubclipseSVN

Going Interactive: Combining Ad-Hoc and Regression Testing

Approach of Unit testing with the help of JUnit

Test Driven Development

ELF WP 2 UML repository instruction

A Practical Guide to Test Case Types in Java

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

Building OWASP ZAP Using Eclipse IDE

Unit Testing. and. JUnit

Tutorial 5: Developing Java applications

Introduction to Version Control in

Testing. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies. CHAPTER AUTHORS Michael Atmadja Zhang Shuai Richard

Expert PHP 5 Tools. Proven enterprise development tools and best practices for designing, coding, testing, and deploying PHP applications.

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Structured Testing in VDM

Introduction to Programming Tools. Anjana & Shankar September,2010

Software Quality Exercise 2

Unit Testing and JUnit

Version Control with Subversion

Ingeniørh. Version Control also known as Configuration Management

A Tutorial on installing and using Eclipse

Chapter 1: Web Services Testing and soapui

creating a text-based editor for eclipse

Enterprise Service Bus

Version Control Tutorial using TortoiseSVN and. TortoiseGit

Software infrastructure for Java development projects

DAVE Usage with SVN. Presentation and Tutorial v 2.0. May, 2014

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Test Driven Development

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

How to Create an Android Application using Eclipse on Windows 7

Unit Testing with zunit

Introduction to Automated Testing

PxPlus Version Control System Using TortoiseSVN. Jane Raymond

INF 111 / CSE 121. Homework 4: Subversion Due Tuesday, July 14, 2009

Announcement. SOFT1902 Software Development Tools. Today s Lecture. Version Control. Multiple iterations. What is Version Control

Lab 0 (Setting up your Development Environment) Week 1

Java Software Quality Tools and techniques

Work. MATLAB Source Control Using Git

Test Automation Integration with Test Management QAComplete

Fail early, fail often, succeed sooner!

Jenkins on Windows with StreamBase

Hands on exercise for

BlueJ Teamwork Tutorial

TESTING WITH JUNIT. Lab 3 : Testing

Tutorial: BlackBerry Object API Application Development. Sybase Unwired Platform 2.2 SP04

For Introduction to Java Programming, 5E By Y. Daniel Liang

Unit Testing & JUnit

Capabilities of a Java Test Execution Framework by Erick Griffin

Introduction to the course, Eclipse and Python

Testing, Debugging, and Verification

Using JUnit in SAP NetWeaver Developer Studio

Android Setup Phase 2

In depth study - Dev teams tooling

CS 4240: Principles of Software Design. Course Introduction. Tom Horton

Unit-testing with JML

Do not start HiQnet London Architect on the computer until instructed to do so below at Start and Configure HiQnet London Architect.

Integrating TAU With Eclipse: A Performance Analysis System in an Integrated Development Environment

MyNetFone Virtual Fax. Virtual Fax Installation

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Source Control and Team-Based Design in System Generator Author: Douang Phanthavong

Measuring the Attack Surfaces of SAP Business Applications

PTC Integrity Eclipse and IBM Rational Development Platform Guide

Meister Going Beyond Maven

Jenkins Continuous Build System. Jesse Bowes CSCI-5828 Spring 2012

Lab work Software Testing

Tutorial IV: Unit Test

Pipeline Orchestration for Test Automation using Extended Buildbot Architecture

Revision control systems (RCS) and

GENiC. Deliverable D5.1 Development & Integration guidelines including integration environment & means. Dissemination Level: Public

Oracle FLEXCUBE Direct Banking Android Tab Client Installation Guide Release

Version Control Using Subversion. Version Control Using Subversion 1 / 27

Nexus Professional Whitepaper. Repository Management: Stages of Adoption

Tutorial: Android Object API Application Development. SAP Mobile Platform 2.3 SP02

SOFTWARE DEVELOPMENT BASICS SED

Testing Tools Content (Manual with Selenium) Levels of Testing

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

Constant Contact Export

Introduction to C Unit Testing (CUnit) Brian Nielsen Arne Skou

Sonatype CLM for Maven. Sonatype CLM for Maven

Eclipse.Net Hosted Librarian Guide

JUnit. Introduction to Unit Testing in Java

Concordion. Concordion. Tomo Popovic, tp0x45 [at] gmail.com

Continuous Integration with Jenkins. Coaching of Programming Teams (EDA270) J. Hembrink and P-G. Stenberg [dt08jh8

Integration of DB oriented CAD systems with Product Lifecycle Management

Department of Veterans Affairs. Open Source Electronic Health Record Services

Hadoop Basics with InfoSphere BigInsights

Software project management. and. Maven

Rational Team Concert. Quick Start Tutorial

How to install and use the File Sharing Outlook Plugin

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

How To Test In Bluej

Subversion Server for Windows

Ce document a été téléchargé depuis le site de Precilog. - Services de test SOA, - Intégration de solutions de test.

Agile.NET Development Test-driven Development using NUnit

Setting up an online Java Jmonitor. server using the. EXPERIMENTAL code from. John Melton GØORX/N6LYT

Transcription:

Introduction to unit testing with Java, Eclipse and Subversion Table of Contents 1. About Unit Tests... 2 1.1. Introduction... 2 1.2. Unit tests frameworks... 3 2. A first test class... 4 2.1. Problem description... 4 2.2. Code organisation... 4 2.3. Test implementation... 6 2.4. Managing the project versions with Subversion... 10 3. Stubs... 14 3.1. A second package... 14 4. Test suites... 17 4.1. Grouping unit tests in suites... 17 2006 FloConsult SPRL 1/18

1. About Unit Tests 1.1. Introduction Unit testing is a key technique in today's agile software development. Unit tests are written by the developer during the coding activity, and thus pertain in the white box testing category: Black box testing input output White box testing input output Unit tests have following characteristics: they are automatic (do not require human intervention during the tests) they are reproducible they are independent from each other their result is either OK (all tests have been successful), or NOK (at least one test failed, in this case details are given to the user about the failure(s)) may be combined in test suites to allow non-regression testing during the development This document will present progressively concrete unit testing cases, using: Java as programming language (http://www.java.com/), Eclipse as Development Environment (http://www.eclipse.org/) Subversion as version control system (http://subversion.tigris.org/), its GUI for Windows TortoiseSVN (http://www.eclipse.org/), and its Subclipse plugin for Eclipse (http://subclipse.tigris.org/). Enterprise Architect as UML Modeling tool (http://www.sparxsystems.com/products/ea.html).. 2006 FloConsult SPRL 2/18

1.2. Unit tests frameworks JUnit is the original Java library for unit testing, ad such frameworks exist nowadays for many programming languages. They all aim at following the software design: Testing client Test ftests TestCase run(testresult) setup() runtest() teardown() TestResult TestSuite run(testresult) addtest(test) ConcreteTestCase setup() runtest() teardown() forall test in ftests test.run(testresult) TestedClass action() setup() runtest() teardown() test1() test2() fname runtest() test1() or test2() test1() and test2() are methods of the concrete test class ConcreteTestCase, one such class being developed per nominal application class (TestClass). They contain code testing the services of this application class. Note that the test-first practice recommend writing those test methods before writing the nominal code, because it helps to keep the design simple from the start (see for example the article of Ron Jeffries http://www.xprogramming.com/xpmag/testfirstguidelines.htm). Besides the test methods, the developer may define for the concrete test case a setup() method, that will be called be the framework before each test method, as well as a teardown() method, called after each test. In JUnit, the default implementation of runtest() uses the Java reflexion to invoke methods with names beginning with test ; runtest() needs being redefined only if you want to override this behavior. The run() template method calls setup(), runtest(), teardown(), what in turns implies concretely the execution of: setup(), test1(), teardown() setup(), test2(), teardown()... The number of tests executed and their result are recorded in the TestResult class. A last important point in this design is the TestSuite notion, that allows calling recursively the run() methods on TestCase classes, allowing to call globally all the tests defined. 2006 FloConsult SPRL 3/18

2. A first test class 2.1. Problem description Let us first think of a Person class, having a birth date as attribute, and an age calculation method: people Person birthdate : Date getage() : int { pre: self.birthdate->exists() post: result = date(today) self.birthdate } <<throws>> utils PreconditionNotRespectedException 2.2. Code organisation This class will be placed in the people package, itself in the src.main.java.floconsult.unittests packages: Note that the directory structure follows the recommendation of the project building tool Maven (http://maven.apache.org/maven-1.x/reference/conventions.html). Using the JUnit enabled menus of Eclipse, we may directly create the associated test case (in the parallel package tree under src\test): 2006 FloConsult SPRL 4/18

Here we chose to place the test class in the test part, and not to redefine the setup() and teardown() methods (the other data is left as proposed by Eclipse): We thus have the following directory organisation in our code directory, which at the same 2006 FloConsult SPRL 5/18

time allows to separate the test classes from the main ones, while placing them in the same package from the java level (here java.floconsult.unittests.people). people PersonTest Person birthdate : Date getage() : int 2.3. Test implementation To run our first tests, we first have to define the tests we think of: If no birth date is set the method should return an exception If person has a birth date such as his/her age would be 30 calculation correct 2006 FloConsult SPRL 6/18

Then, to allow compiling the test code, let us define the skeleton (without method implementation) of the Person class: Service offered by the Person class (not yet implemented) Running the PersonTest unit tests: We have (as awaited as the getageinyears()) method is not yet implemented) both tests fail: 2006 FloConsult SPRL 7/18

We are ready now for implementing the method: 2006 FloConsult SPRL 8/18

What allow us to see our first JUnit green bar, indicating the success of our 2 tests: Before detailing the notion of test suites and of stubs, let us take a look at the version management of our project files using Subversion (SVN): 2006 FloConsult SPRL 9/18

2.4. Managing the project versions with Subversion Considering we place the project in Subversion before implementing the method: Then the user has to select the SVN repository to use (here file:///c:/localsvnrepo/), and confirm the project import into this repository. This done, the project may be seen in the SVN Repository view in Eclipse: SVN version number of the file in the repository 2006 FloConsult SPRL 10/18

Or directly in the Package Explorer view: DB icon indicates the Subversion link (becomes a star when file modified and not yet committed in Subversion) After the successful run of the tests, it is a good rule of thumb to commit the modifications in the SVN repository: 2006 FloConsult SPRL 11/18

As soon as a file is managed under Subversion, we may look at its history in Subversion: Selecting 2 versions, we may compare the files: 2006 FloConsult SPRL 12/18

This to demonstrate the very nice integration of Subversion in Eclipse (using the plug-in given in #1.1.Introduction). 2006 FloConsult SPRL 13/18

3. Stubs 3.1. A second package Let us now consider a package company, with an Employee, being a Person, and having a salary category, which, allows to calculate his/her salary: company EmployeeTest <<interface>> SalaryMgr Employee salarycategory : int = [1,2,3] <<uses>> getsalaryforcategory(cat) : int getcurrentsalary() : int people Person birthdate : Date getage() : int The calculation of the salary needs a class implementing the interface SalaryMgr, which is not detailed here. To unit test the Employee class, we cannot afford having a dependence on an external unknown class, and we thus have to introduce an implementation of SalaryMgr, having a perfectly predictable behaviour. This is a Stub as represented hereunder: company EmployeeTest Employee salarycategory : int = [1,2,3] getcurrentsalary() : int <<uses>> <<interface>> SalaryMgr getsalaryforcategory(cat) : int <<stub>> SalaryMgrStub getsalaryforcategory(cat) : int { if cat=1 (JUNIOR) return 1000 if cat=2 (SENIOR) return 3000 if cat=3 (MANAGER) return 5000} 2006 FloConsult SPRL 14/18

We will place this stub in the test part of the project directory, in the company package: What allow us to test the getcurrentsalary() method of the Employee class: 2006 FloConsult SPRL 15/18

This Stub is instantiated in the SetUp() method of the EmployeeTest class (recall that this method is called before each test execution), and passed to the getcurrentsalary() method to allow testing it: Obviously, in this very simple example, Employee does not add any behaviour to the one of the SalaryMgr, but this technique of stubbing is applicable to any situation were we want our classes, and thus our unit tests, to be independent of external (unpredictable) behaviours. 2006 FloConsult SPRL 16/18

4. Test suites 4.1. Grouping unit tests in suites A last important notion in this introduction to unit testing with Java and Eclipse is the notion of test suites. In the previous paragraphs, we successively tested the Person class of the people package (2 tests), and the Employee class of the company package (3 tests). As said in the introduction, unit tests may be combined in suites, allowing to (re-)execute all the existing unit tests, and to verify that the system did not regress following modifications or additions in the code. Again, Eclipse offers a wizard to ease the creation of test suites. First, let us create the test suites at the people level: 2006 FloConsult SPRL 17/18

This suite class executes all the test classes in the package: We similarly create a test suite at the company level, and then a parent test suite at the higher level, which call the lower-level test suites. Executing this suite runs the 5 tests at once: 2006 FloConsult SPRL 18/18