Unit Testing & JUnit



Similar documents
Java Unit testing with JUnit 4.x in Eclipse

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

Testing Methodology Assignment 1 Unit testing using JUnit

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

Unit Testing and JUnit

Effective unit testing with JUnit

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Approach of Unit testing with the help of JUnit

TESTING WITH JUNIT. Lab 3 : Testing

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Writing Self-testing Java Classes with SelfTest

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CSE 326: Data Structures. Java Generics & JUnit. Section notes, 4/10/08 slides by Hal Perkins

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Testing, Debugging, and Verification

JUnit - A Whole Lot of Testing Going On

Unit Testing JUnit and Clover

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

Tools for Integration Testing

Unit Testing. and. JUnit

Agile.NET Development Test-driven Development using NUnit

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Java course - IAG0040. Unit testing & Agile Software Development

AP Computer Science Java Subset

CS 111 Classes I 1. Software Organization View to this point:

1 White-Box Testing by Stubs and Drivers

RUP. Development Process. Iterative Process (spiral) Waterfall Development Process. Agile Development Process. Well-known development processes

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

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

ID TECH UniMag Android SDK User Manual

Development Environment and Tools for Java. Brian Hughes IBM

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

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

Project 4 DB A Simple database program

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

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

Introduction to Java

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

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Lab work Software Testing

Overview of Web Services API

Experimental Comparison of Concolic and Random Testing for Java Card Applets

Fail early, fail often, succeed sooner!

Creating A Walking Skeleton

How To Test In Bluej

Going Interactive: Combining Ad-Hoc and Regression Testing

Pemrograman Dasar. Basic Elements Of Java

Java CPD (I) Frans Coenen Department of Computer Science

A Practical Guide to Test Case Types in Java

Unit-testing with JML

CSE 308. Coding Conventions. Reference

Appium mobile test automation

Comparing the effectiveness of automated test generation tools EVOSUITE and Tpalus

D06 PROGRAMMING with JAVA

Software documentation systems

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

Instrumentation Software Profiling

Unit Testing with junit. based on materials by M. Stepp, M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia

Sources: On the Web: Slides will be available on:

Performance Testing from User Perspective through Front End Software Testing Conference, 2013

11 November

Software Development Environment. Installation Guide

ECE 122. Engineering Problem Solving with Java

Crash Course in Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

UI Performance Monitoring

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

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

CmpSci 187: Programming with Data Structures Spring 2015

MXwendler Javascript Interface Description Version 2.3

Tutorial - How to Use Lotus Domino Web Services in Java Development

TeCReVis: A Tool for Test Coverage and Test Redundancy Visualization

Tutorial on OpenCV for Android Setup

Part I. Multiple Choice Questions (2 points each):

Using NetBeans IDE to Build Quick UI s Ray Hylock, GISo Tutorial 3/8/2011

With a single download, the ADT Bundle includes everything you need to begin developing apps:

Algorithms and Data Structures Written Exam Proposed SOLUTION

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Test Driven Development

Visual Basic Programming. An Introduction

VHDL Test Bench Tutorial

Chapter 8 Software Testing

Java Interview Questions and Answers

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Habanero Extreme Scale Software Research Project

Moving from CS 61A Scheme to CS 61B Java

7.1 Our Current Model

Chapter 2 Introduction to Java programming

Introduction to Java. CS 3: Computer Programming in Java

Object Oriented Software Design

Java Bluetooth stack Acceptance Test Plan Version 1.0

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Transcription:

Unit Testing & JUnit

Lecture Outline Communicating your Classes Introduction to JUnit4 Selecting test cases

UML Class Diagrams Rectangle height : int width : int resize(double,double) getarea(): int getperimeter():int Give an external view of a class But there are lots of questions they don t answer If you want to use this class, you will need more information For example, what does the resize method do? What do its parameters mean?

More information Method signature public void resize( double hscale, double wscale) JavaDoc /** * Grow or shrink a rectangle in two dimensions. * * @param hscale double multiplier for height * @param wscale double multiplier for width */

(Even) More information More JavaDoc /** * Grow or shrink a rectangle in both dimensions * scaling dimensions are floats (can be less than 1) * but final dimensions are recast as integers. * * @param hscale the float multiplier for height. * @param wscale the float multiplier for width. * @throws exceptions for what cases?? */

Examples of Method Behaviour Inputs height=3, width=4 hscale=2.0, wscale=0.5 height=3, width=4 hscale=1.0, wscale=1.0 height=3, width=4 hscale=0.5, wscale=0.0 Output height=6, width=2 height=3, width=4 height=1??, width=0??

How much info do you need? Method signatures don t tell you what the method does JavaDoc can specify anything you need to know, but we don t have a way of checking that the code meets the specification Examples are helpful for programmers Most importantly, we can check whether code behaves as the examples claim When such checks are themselves Java code, then we can recheck that code still works after any change, by running the test code

Lecture Outline Communicating Java methods Introduction to JUnit4 Selecting test cases

JUnit JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java. JUnit is Open Source Software, released under the Common Public License Version 1.0 and hosted on SourceForge. There is an excellent Eclipse plug-in to support the latest JUnit version 4.1

JUnit de facto standard unit testing framework bundled in most IDEs http://www.junit.org/ useful to have one complete test suite the programmers can run with a pass fail result automatic quality assurance: when the code changes, test cases are rerun to check that nothing breaks

Automated Testing Tests must run automatically without any human participation beyond launching the test suite (and maybe not even that) Results should be pass/fail Test failures need to be blindingly obvious Test must be reproducible and consistent

What s in a test case? Unit test cases have the following form: 1. describe the method s inputs 2. describe the expected outputs 3. call the method and observe actual output 4. compare expected with actual and report

Coding a test case in JUnit4 @Test public void testgetareanormal() { //1. describe input Rectangle r1 = new Rectangle(3,4); //2. describe expected output(s) int expected = 12; //3. call the method int actual = r1.getarea(); // 4. compare actual output with the expected output assertequals("normal getarea",expected,actual); }

Before and After every test case @Before public void setup() throws Exception { //TODO Create objects and common variables //for your tests here //setup is called before EACH test case is run } @After public void teardown() throws Exception { //TODO put code here to reset or //release objects //eg. p1=null; //to garbage collect old MobilePhones //teardown is called after EACH test case is run }

How a test suite is executed 1. @Before code 2. @Test case 1 3. @After code 4. @Before code 5. @Test case 2 6. @After code etc. until all test cases are executed IMPORTANT each test is independent of the previous ones

import static org.junit.assert.*; import org.junit.after; import org.junit.before; import org.junit.test; public class BankAccountTest { BankAccount b0; @Before public void setup() throws Exception { b0 = new BankAccount( rachel,0); //create an empty BA } @After public void teardown() throws Exception { b0=null; //garbage collection for old BankAccount } } @Test public void testdepositnormal() { b0.deposit(100); assertequals(b0.getbalance(),100); } @Test public void testdepositspecial() { b0.deposit(0); assertequals(b0.getbalance(),0); } @Test public void testdepositillegal() { //TODO test illegal deposit amounts fail("not yet implemented"); }

Does expected = actual? To compare any variables of the 8 Java base types (boolean, byte, short, int, long, char, float, double) use assertequals(expected, actual) First argument (optional) is a string that's printed if the assertion fails: assertequals(message, expected, actual)

Straight-forward comparisons for basic types public static void assertequals (int expected, int actual) assertequals(boolean expected, boolean actual) assertequals(byte expected, byte actual) assertequals(char expected, char actual) assertequals(short expected, short actual) assertequals(long expected, long actual)

Floating point arithmetic As always with floating point arithmetic (double or float), you want to avoid direct equality comparisons. public static void assertequals( double expected, double actual, double tolerance) For example, assertequals( 0.552, 0.510, 0.1) is true but assertequals( 0.552, 0.510, 0.01) is false since the values are equivalent to within a tolerance of plus or minus 0.1 but not to 2 DP

How to Test Objects What is the expected result of this test? @Test public void testobjequality() { Rectangle r1 = new Rectangle(2,4); Rectangle r2 = new Rectangle(2,4); assertequals(r1,r2); }

Testing Objects Problem JUnit responds with junit.framework.assertionfailederror: resize OK expected:<rectangle@913fe2> but was:<rectangle@1f934ad> Problem: we have tested for equivalence of the references to the 2 rectangle objects, not the contents of those rectangle objects

Solution Write a local equivalence tester for the class you are testing private void testrectangleequality( Rectangle r1, Rectangle r2) { assertequals( Not same height,r1.getheight(),r2.getheight()); assertequals( Not same width,r1.getwidth(),r2.getwidth()); } Aside: This test method exits after the first failure, so if both tests fail only the first is reported

More assertions assertfalse(condition) assertfalse(message, condition) assertnotnull(object) assertnotnull(message, object) 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)

JUnit4 in Eclipse (Demo) Start Eclipse and check that JUnit is on the project build path Java Settings > Libraries Select the class to be tested in the Navigator window e.g. BankAccount.java Right click and select New->JUnit Test Case An outline class BankAccountTest.java will be generated automatically for you Just fill in the values to test and go!

Lecture Outline Communicating Java methods Introduction to JUnit Selecting test cases

Choosing Test Cases Typical cases Sanity check that method code works as expected, but try to choose tricky cases anyway! Special cases and Boundary cases Most errors occur for boundary values, eg empty or full array, -1,0,1 etc. Exceptional cases Illegal input, divide by 0, un-initialized parameters

Tables are a good way of planning your test cases Inputs TYPICAL height=3, width=4 hscale=2.0, wscale=0.5 BOUNDARY height=3, width=4 hscale=1.0, wscale=1.0 EXCEPTIONAL height=3, width=4 hscale=0.5, wscale=0.0 Output height=6, width=2 height=3, width=4 height=1??, width=0??

Also, ask questions Works well when testing someone else s class eg. a library class Ask: What happens if you do XXX? Is an exception thrown? If so which one? Is null treated as zero? Is null returned? Something else? Write a test that determines the answer to your question(s)

Future Work beyond JUnit Test Log Files Goal: Generate a human-readable text file, detailing the tests run and their results Implementation: add (guarded) print statements to your JUnit Test file Acceptance Tests Use a main method that executes a sequence of steps (method calls) corresponding to client use cases for the system Coverage Testing Check that your test cases execute all branches of the code Refs: JNuke Virginia test environment etc.