Unit-testing with JML



Similar documents
Unit Testing JUnit and Clover

Unit Testing and JUnit

The junit Unit Tes(ng Tool for Java

Approach of Unit testing with the help of JUnit

Unit Testing. and. JUnit

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

Java course - IAG0040. Unit testing & Agile Software Development

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

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

TESTING WITH JUNIT. Lab 3 : Testing

Software Engineering Techniques

Test Driven Development

Rigorous Software Development CSCI-GA

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

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

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

JUnit. Introduction to Unit Testing in Java

An Overview of JML Tools and Applications

UNIT TESTING. Written by Patrick Kua Oracle Australian Development Centre Oracle Corporation

JUnit - A Whole Lot of Testing Going On

Tutorial for Spring DAO with JDBC

Unit Testing & JUnit

Capabilities of a Java Test Execution Framework by Erick Griffin

Using JUnit in SAP NetWeaver Developer Studio

Testing, Debugging, and Verification

Crash Course in Java

Java Interview Questions and Answers

Tools for Integration Testing

Design by Contract beyond class modelling

Tool-Assisted Unit-Test Generation and Selection Based on Operational Abstractions

Tutorial on Writing Modular Programs in Scala

Object Oriented Software Design

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

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

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Effective unit testing with JUnit

Monitoring Java Code Using ConGu

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

J a v a Quiz (Unit 3, Test 0 Practice)

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Rigorous Software Engineering Hoare Logic and Design by Contracts

Object Oriented Software Design

Test Automation Integration with Test Management QAComplete

Java Unit testing with JUnit 4.x in Eclipse

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

Using JML to protect Java code against SQL injection. Johan Janssen June 26, 2007

Formal Specification and Verification of Avionics Software

CS 451 Software Engineering Winter 2009

StaRVOOrS: A Tool for Combined Static and Runtime Verification of Java

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

Writing Self-testing Java Classes with SelfTest

Data Flow Static Code Analysis Best Practices

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

Running and Testing Java EE Applications in Embedded Mode with JupEEter Framework

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

An overview of JML tools and applications

Software Testing. Quality & Testing. Software Testing

Introduction to unit testing with Java, Eclipse and Subversion

Plugin JUnit. Contents. Mikaël Marche. November 18, 2005

The C Programming Language course syllabus associate level

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

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

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

Test-Driven Development

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

Tutorial IV: Unit Test

Chapter 8 Software Testing

Testing Tools Content (Manual with Selenium) Levels of Testing

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

How To Write A Test Engine For A Microsoft Microsoft Web Browser (Php) For A Web Browser For A Non-Procedural Reason)

Chapter 1: Web Services Testing and soapui

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Example of a Java program

Java Programming Language

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Integrated Error-Detection Techniques: Find More Bugs in Java Applications

Java: overview by example

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Examples of Design by Contract in Java

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

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

Hoare-Style Monitors for Java

Evaluation of AgitarOne

An overview of JML tools and applications

Sample CSE8A midterm Multiple Choice (circle one)

Transcription:

Métodos Formais em Engenharia de Software Unit-testing with JML José Carlos Bacelar Almeida Departamento de Informática Universidade do Minho MI/MEI 2008/2009 1 Talk Outline Unit Testing - software testing - JUnit framework - testing coverage JML-Unit - basic usage - tool support 2

Software Testing Goal: detect software failures so that defects may be corrected Problem: it is often impossible to test software systems under all possible inputs and admissible states Testing is able to signal the presence of faults, but can t demonstrate their absence Usually performed on different levels: - unit testing - functional testing - integration testing - system testing, acceptance testing,... Important to allow an early fault-detection. Time Detected Requirements Architecture Construction System Test Post-Release Time Introduced Requirements 1! 3! 5 10! 10! 10 100! Architecture - 1! 10! 15! 25 100! Construction - - 1! 10! 10 25! 3 Unit Testing Tests the minimal software component, or module (e.g. a Java class). Isolate each part of the program and show that the individual parts are correct. Unit test cases embody characteristics that are critical to the success of the unit --- to some extent, they act as specifications of appropriate/inappropriate uses. Typically done by software developers to ensure that the code meets software requirements and behaves as intended. Good unit test design produces test cases that cover all paths through the unit with attention paid to loop conditions. It relies on a sustainable process for ensuring that test case failures are reviewed daily and addressed immediately. Dependencies with other parts of the system (e.g. databases) is abstracted by mock-objects. 4

JUnit framework Java framework that supports unit-testing. Some terminology: - Test Case - set of conditions/variables/invocations that exercises the target code - Assertions - act of comparing the outcome of tests with expected results - Fixture - appropriate environment for running the test cases - Test Suite - collection of test cases - Test Runner - program that runs the tests Availability: - http://www.junit.org/ - current version: 4.5 obs.: version 4.X uses Java annotations (a Java5 feature...). Thus, when using JUnit and JML, it is preferable to use version 3.8.2. 5 JUnit usage JUnit is Java framework to assist programmers in writing unittests for their code Basic usage steps: - write a TestCase - write a TestSuite - run the tests Benefits of unit testing greatly depend on: - programmers commitment in writing quality test cases - organisational procedures for evaluate and monitorise tests (build system; code/tests organization;...) Recommended reading: - JUnit Primer - (http://clarkware.com/articles/ JUnitPrimer.html) - JUnit - A Cook's Tour (http://junit.sourceforge.net/doc/ cookstour/cookstour.htm) 6

TestCase classes Collects the tests for the intended code unit Extends JUnit TestCase abstract class Each test is a public method with name testxxx() Outcome of tests is checked against expected values (assert methods) - asserttrue/false, assert(not)equals, assert(not)same - assertarray(not)equals - fail (Test Fixture) initialisation of common objects under test is performed by overriding method setup() ( teardown() to release them). (optional) static method suite() defines the default TestSuite for the test methods. 7 TestCase example import junit.framework.testcase; public class ShoppingCartTest extends TestCase { private ShoppingCart cart; private Product book1; protected void setup() { cart = new ShoppingCart(); book1 = new Product("Pragmatic Unit Testing", 29.95); cart.additem(book1); /** * Tests emptying the cart. */ public void testempty() { cart.empty(); assertequals(0, cart.getitemcount());... // collects test methods from this class... public static Test suite() { TestSuite suite= new TestSuite(MoneyTest.class); return suite; 8

TestSuite classes TestSuite is the smallest execution unit in JUnit Is a composite of other tests (either TestCases or TestSuites) Adopt often an hierarchical structure (mirroring the package structure) import junit.framework.test; import junit.framework.testsuite; public class EcommerceTestSuite { public static Test suite() { TestSuite suite = new TestSuite(); suite.addtestsuite(shoppingcarttest.class); suite.addtest(creditcardtestsuite.suite()); return suite; /** * Runs the test suite using the textual runner. */ public static void main(string[] args) { junit.textui.testrunner.run(suite()); 9 Code Coverage One of the most widely used approach to measure software testing quality is the adoption of some coverage measure Different possible measurements: - function coverage (functions tested) - line coverage (lines of code exercised by tests) - branch coverage (coverage of decision points) - path coverage (control flow paths) - entry/exit coverage,... Like any software metric, results should be taken with caution: - can be blind to that obvious problem - often very easy to trick Several (free) tools available (e.g. Emma) 10

JML-Unit A Behaviour Interface Specification Language for Java (Gary T. Leavens et al. [BCC+05]) It permits to: - specify behaviour of Java classes - record design & implementation decisions...by adding assertions to Java source code JML syntax is well integrated with Java: - JML assertions are added as comments in.java files, between /*@... @*/, or after //@ ; - Properties are specified as Java boolean expressions, extended with some operators (\old, \forall, \result,... ), -...and some keywords (requires, ensures, signals, assignable, pure, invariant, non_null,... ). 11 JML-Unit usage Pre and postconditions for methods are established through the requires and ensures clauses: /*@ requires amount >= 0; @ ensures balance == \old(balance)-amount; @ ensures \result == balance; @*/ public int debit(int amount) {... where - \old(balance) refers to the value of balance before the execution of the method; - the multiple ensures clauses are equivalent to their conjunction; - \result refers to the outcome of the method (return value). 12

JML-Unit (data generators) JML properties are boolean Java expressions......with the proviso that their evaluation is side-effect free (i.e. does not change the internal state). A method without side-effects is called pure. Programmers might signal methods as pure: public /*@ pure @*/ int getbalance(){... Directory /*@ pure non_null @*/ getparent(){... The non_null clause signals that the result of getparent() can t be null (can also be used in arguments and instance variables). JML property language is extended with binding operators: \forall, \exists, \sum, \product, \max, \min,... E.g. (\forall int i ; 0<=i && i<n ; a[i]==null) 13 JML-Unit Unit tests are built around - input data - code execution - result check JML runtime assertion check is clearly interesting for checking successful test results: a method call is successful whenever its post-condition is valid (+class invariant) (if input data validates precondition...) JmlUnit adds to the equation JUnit+JML some ingredients: - systematically generates (JUnit) TestCases that exercise all public class methods; - preconditions are used to filter out irrelevant method calls - post-conditions/invariant violations capture test failures...the user only needs to provide interesting input data 14

JMLUnit usage 1st step: setup CLASSPATH - include jmlruntime.jar, jmljunitruntime.jar, junit.jar 2nd step: run jmlunit on the target jml-annotated java file $ jmlunit MyJmlClass.java generates MyJmlClass_JML_Test.java and MyJmlClass_JML_TestData.java 3rd step: add test-values to MyJmlClass_JML_TestData.java 4th step: java-compile test case $ javac MyJmlClass_JML_*.java 5th step: jml-compile MyJmlClass.java $ jmlc MyJmlClass.java 6th step: run the tests $ jml-junit MyJmlClass_JML_TestCase 15 Test Data Edit the MyJmlClass_JML_TestData.java file: Clonable objects (Strings, ints,...) private org.jmlspecs.jmlunit.strategies.strategytype vjava_lang_stringstrategy = new org.jmlspecs.jmlunit.strategies.stringstrategy() {protected java.lang.object[] adddata() { return new java.lang.string[] { // replace this comment with test data if desired "Jose", "Maria", "Manuel" ; ; Non-Clonable objects private org.jmlspecs.jmlunit.strategies.strategytype vuserstrategy = new org.jmlspecs.jmlunit.strategies.newobjectabstractstrategy() { protected Object make(int n) { switch (n) { // replace this comment with test data if desired case 0: return new User( user1, pass1 ); case 1: return new... //... 16