Effective unit testing with JUnit



Similar documents
Unit Testing and JUnit

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

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

Approach of Unit testing with the help of JUnit

JUnit - A Whole Lot of Testing Going On

Test Driven Development

Unit Testing JUnit and Clover

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

JUnit. Introduction to Unit Testing in Java

Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

Unit Testing & JUnit

Extreme Programming and Embedded Software Development

Tutorial IV: Unit Test

Tutorial 7 Unit Test and Web service deployment

TESTING WITH JUNIT. Lab 3 : Testing

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

Introduction. Motivational Principles. An Introduction to extreme Programming. Jonathan I. Maletic, Ph.D.

Java course - IAG0040. Unit testing & Agile Software Development

Test-Driven Development

Xtreme RUP. Ne t BJECTIVES. Lightening Up the Rational Unified Process. 2/9/2001 Copyright 2001 Net Objectives 1. Agenda

Capabilities of a Java Test Execution Framework by Erick Griffin

Unit Testing. and. JUnit

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

Test Driven Development Part III: Continuous Integration Venkat Subramaniam

Java Unit testing with JUnit 4.x in Eclipse

Agile.NET Development Test-driven Development using NUnit

Software Development Tools

Using JUnit in SAP NetWeaver Developer Studio

Advanced Test-Driven Development

Tutorial for Spring DAO with JDBC

SUnit Explained. 1. Testing and Tests. Stéphane Ducasse

Merlin A Continuous Integration Tool for VisualWorks

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

Web Services API Developer Guide

XP & Scrum. extreme Programming. XP Roles, cont!d. XP Roles. Functional Tests. project stays on course. about the stories

Chapter 1: Web Services Testing and soapui

Test Automation Integration with Test Management QAComplete

Desktop, Web and Mobile Testing Tutorials

Test Driven Development

Software Engineering Techniques

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

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

Introduction to Automated Testing

Java Bluetooth stack Acceptance Test Plan Version 1.0

Going Interactive: Combining Ad-Hoc and Regression Testing

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

Java Application Developer Certificate Program Competencies

Agile Testing and Extreme Programming

.NET and J2EE Intro to Software Engineering

JDemo - Lightweight Exploratory Developer Testing

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

Automated Integration Testing & Continuous Integration for webmethods

Web Applications Testing

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

Load Testing with JMeter

Advanced Software Testing

Agile and Secure: Can We Be Both?

Leveraging Rational Team Concert's build capabilities for Continuous Integration

Security Testing Web Applications throughout Automated Software Tests

Applets, RMI, JDBC Exam Review

A Practical Guide to Test Case Types in Java

Unit Testing with FlexUnit. by John Mason

Jenkins TestLink Plug-in Tutorial

How To Write A Map In Java (Java) On A Microsoft Powerbook 2.5 (Ahem) On An Ipa (Aeso) Or Ipa 2.4 (Aseo) On Your Computer Or Your Computer

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

AUTOMATING THE WEB APPLICATIONS USING THE SELENIUM RC

The junit Unit Tes(ng Tool for Java

Rake Task Management Essentials

CS193j, Stanford Handout #10 OOP 3

Fail early, fail often, succeed sooner!

Fundamentals of Java Programming

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

CS506 Web Design and Development Solved Online Quiz No. 01

Division of Informatics, University of Edinburgh

This section provides a 'Quickstart' guide to using TestDriven.NET any version of Microsoft Visual Studio.NET

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

Creating a Simple, Multithreaded Chat System with Java

Testing Tools and Techniques

1 Hour, Closed Notes, Browser open to Java API docs is OK

Testing, Debugging, and Verification

Driving a Time-based Simulation from MATLAB for Custom Calculations and Control

Load-Testing to Find and Fix Scalability Problems

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

History OOP languages Year Language 1967 Simula Smalltalk

Domain Specific Languages for Selenium tests

Unit Testing with JUnit and CppUnit

Continuous Integration Part 2

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

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

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

Transcription:

Effective unit testing with JUnit written by Eric M. Burke burke_e@ociweb.com Copyright 2000, Eric M. Burke and All rights reserved last revised 12 Oct 2000 extreme Testing 1

What is extreme Programming (XP) A lightweight software development methodology best for small to medium teams (less than 20 people) coding and testing are key activities constant code reviews through pair programming refactoring is done constantly focus on simplicity continuous integration testing short iterations This is not a presentation on XP extreme Testing 2

XP Testing Programmers write unit tests if the interface for a method is unclear if an implementation will be complicated to test unusual inputs and boundary conditions before refactoring all unit tests must run at 100% "Customers" write functional tests may require a dedicated tester to help failed functional tests can help determine priorities extreme Testing 3

Write unit tests before code helps you to think about the interface makes it easy to determine when the code is finished just write enough code to solve the current problem When a bug occurs first write a unit test to expose the bug then fix the bug then run the test again Unit Test Process XP XP discourages heavy emphasis on on "framework" development early early on. on. Focus on on the the simplest solution that that will will solve today's problems. Reusable code code will will emerge as as the the result of of refactoring. extreme Testing 4

Don't test trivial things Good Test Design like simple getter/setter methods will ultimately discourage programmers from writing good tests Test anything that may go wrong Tests become part of the system documentation Tests must be automated pass/fail don't make someone interpret the results extreme Testing 5

JUnit Overview A free Java tool for writing unit tests www.junit.org visit www.xprogramming.com for other languages C++, Delphi, Eiffel, Forte 4GL, Objective-C, Perl, PowerBuilder, Python, Smalltalk, Visual Basic, others... Supports batch mode and GUI mode testing Test cases can be grouped into test suites All tests are pass/fail type tests stack traces and error messages indicate where failures occur extreme Testing 6

JUnit Design junit.framework Assert <<interface>> Test * TestCase TestSuite MyTest This is what you write extreme Testing 7

Writing a Unit Test Create a subclass of TestCase import junit.framework.*; public class HelloWorldTest extends TestCase { public HelloWorldTest(String name) { super(name); Write individual test methods numerous assert(...) methods are available public void testsample1() { String s1 = "somestring"; String s2 = "somestring"; assertequals(s1, s2); extreme Testing 8

Writing a Unit Test (Cont'd) Rules for test methods must be public, and should not take arguments should begin with the name "test" so reflection can determine which methods to call may optionally throw exceptions will be reported as errors by JUnit asserts are reported as failures it is OK to use assert several times in the same method when an assert condition is not met, the current method is finished extreme Testing 9

Writing a Unit Test (Cont'd) Include a method called suite() again, reflection locates this method public static Test suite() { TestSuite suite = new TestSuite(); suite.addtest(new HelloWorldTest("testSample1")); suite.addtest(new HelloWorldTest("testSample2")); suite.addtest(new HelloWorldTest("testSample3")); return suite; A main(...) method is convenient for batch tests public static void main(string[] args) { junit.textui.testrunner.run(suite()); extreme Testing 10

Running the Tests For batch mode testing java HelloWorldTest uses the main() method shown on the previous page java junit.textui.testrunner HelloWorldTest For Swing GUI testing java junit.swingui.testrunner SomeTest java junit.swingui.loadingtestrunner SomeTest reloads classes when they change; won't work with JARs Older AWT GUI is in the junit.ui package no dropdown list of previous tests no test browser capability extreme Testing 11

JUnit Demo extreme Testing 12

Fixtures Other JUnit Features a convenient way to set up data for multiple tests the fixture can fail just like a test by calling assert, fail, or by throwing an exception see example code on next page Using Java reflection to construct a TestSuite you can create a TestSuite in a single line of code automatically adds all test* methods Test mysuite = new TestSuite(MyTester.class); extreme Testing 13

Example Fixture The setup and teardown methods are called before an after every test method invocation public class FixtureDemo extends TestCase { private FileReader demoreader; // IOException is only required for this example protected void setup() throws IOException { demoreader = new FileReader("demo.dat"); protected void teardown() throws IOException { demoreader.close(); extreme Testing 14

junit.extensions.testsetup Allows you to setup once before a batch of tests and teardown once at the end... public static Test suite() { TestSuite suite = new TestSuite(TestSetupDemo.class); return new TestSetup(suite) { ; public void setup() { System.out.println("oneTimeSetup"); public void teardown() { System.out.println("oneTimeTeardown"); extreme Testing 15

Setting up a RepeatingTest Use junit.extensions.repeatedtest public static void main(string[] args) { TestSuite mysuite = new TestSuite(); mysuite.addtest(new MyTest("testSample1")); mysuite.addtest(new MyTest("testSample3")); // first parameter is a Test, so it doesn't // have to be a TestSuite Test repeater = new RepeatedTest(mySuite, 1000); junit.textui.testrunner.run(repeater); output:... Time: 0.741 OK (2000 tests) extreme Testing 16

Threading Use junit.extensions.activetest could not find examples; documentation is sparse won't work with the command line TestRunner as soon as the test is finished, TestRunner executes System.exit(0) the threaded test appears to finish immediately, as soon as the Thread object is started Swing GUI doesn't repaint properly if individual threads run very fast when I inserted sleep() statements, the GUI repainted extreme Testing 17

Threading Example public static Test suite() { Test repeater1 = new RepeatedTest( new ThreadDemo("testSample1"), 100); Test repeater2 = new RepeatedTest( new ThreadDemo("testSample2"), 50); // run the repeaters in parallel Test thread1 = new ActiveTest(repeater1); Test thread2 = new ActiveTest(repeater2); TestSuite suite = new TestSuite(); suite.addtest(thread1); suite.addtest(thread2); return suite; extreme Testing 18

Assert Summary assert(boolean condition) // ensures that condition is true assert(string message, boolean condition) all remaining assert(...) methods have an optional message parameter assertequals(double expected, double actual) overloaded for long, Object assertnotnull(object obj) assertnull(object obj) assertsame(object expected, Object actual) fail() fail(string message) == comparison extreme Testing 19

Testing Tips JUnit is really only designed for unit tests you may need other tools for integration testing or load testing Some things, like Servlets, are very hard to test you could simulate an HTTP request to the server your test program is pretending to be a browser or create an implementation of the HttpServletRequest interface, and invoke your Servlet without a server the discussion forum mentioned on the next page has links to example code which does this you should separate business logic from your Servlet anyway most tests will be written against the domain objects, rather than the Servlet itself extreme Testing 20

Learning More extreme Programming explained Kent Beck, Addison Wesley a short chapter on unit testing; JUnit is not covered Refactoring Martin Fowler, Addison Wesley the chapter on unit testing is an excellent JUnit intro http://www.c2.com/cgi/wiki?javaunit discussion forum http://www.extremeprogramming.org extreme Testing 21