Effective Testing Brian M. Coyner Senior Software Engineer

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Unit Testing Australian Development Centre Brisbane, Australia.
J-Unit Framework.
Apache Struts Technology
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
JUnit Syed Nabeel. Motivation Unit Testing Responsibility of  developer Rarely done properly Developers Excuse: “I am too much in a hurry”
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
Slide 1 of 9 Presenting 24x7 Scheduler The art of computer automation Press PageDown key or click to advance.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Programmer Testing Testing all things Java using JUnit and extensions.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
© 2005 by IBM; made available under the EPL v1.0 | March 1, 2005 Tim deBoer Gorkem Ercan Extend WTP Server Tools for your.
Software Engineering 2003 Jyrki Nummenmaa 1 CASE Tools CASE = Computer-Aided Software Engineering A set of tools to (optimally) assist in each.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Testing in Extreme Programming
Junit At the forefront of Test Driven Development.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
A S P. Outline  The introduction of ASP  Why we choose ASP  How ASP works  Basic syntax rule of ASP  ASP’S object model  Limitations of ASP  Summary.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Usability Overview Upsorn Praphamontripong CS
Progress Apama Fundamentals
MASS Java Documentation, Verification, and Testing
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
JRA2: Acceptance Testing senarious
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Play Framework: Introduction
Outline SOAP and Web Services in relation to Distributed Objects
Unit testing Java programs Using JUnit
Software Development Tools
Outline SOAP and Web Services in relation to Distributed Objects
Introduction to J2EE Architecture
PHP / MySQL Introduction
An Automated Testing Framework
History, Characteristics and Frameworks
Mocking Your Objects with Impunity
Test-driven development (TDD)
Lecture 1: Multi-tier Architecture Overview
Introduction to JUnit CS 4501 / 6501 Software Testing
Objectives In this lesson you will learn about: Need for servlets
An Introduction to Software Architecture
Introduction to JUnit IT323 – Software Engineering II
Understanding and Designing with EJB
CS 240 – Advanced Programming Concepts
Introducing Java.
Designing For Testability
Joel Adams and Jeremy Frens Calvin College
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Continuous Integration
Principles of Object Oriented Programming
Presentation transcript:

Effective Testing Brian M. Coyner Senior Software Engineer Object Computing, Inc. Co-Author of O’Reilly’s Java Extreme Programming Cookbook 11/19/2018 Effective Testing

References Java Extreme Programming Cookbook Eric M. Burke and Brian M. Coyner O’Reilly http://www.oreilly.com/catalog/jextprockbk/ Available March 2003 Information contained in this presentation is courtesy of O’Reilly’s Java Extreme Programming Cookbook book. Effective Testing

Introduction Introduction To Testing Continuous Integration Getting Started With JUnit Mock Objects Testing Swing Code Testing Server Side Code Wrap Up Effective Testing

Who, What, When, How?? Who should test? What is a test? Everyone What is a test? Pass or Fail Automated What should you test? Everything possible When do you test? New code/ Refactored code How do you test? Practice (lots of it) More to come… No human interpretation Testing is an art Effective Testing

Why Tests Are Important Confidence your code works Confidence when refactoring Confidence other programmers will not break anything Some programmers feel it’s not important to test the “simple” stuff. THIS IS WRONG! It’s the “simple” stuff that usually breaks. Therefore writing tests provides confidence that new features do not break existing code. Effective Testing

Continuous Integration Fully automated build and test process http://martinfowler.com/articles/continuousIntegration.html Making it work Source control repository All developers must have read privileges Example: CVS, StarTeam Automated build process Execute with one command Ant is the tool of choice for Java projects Automated test process Execute entire test suite with one command Ant’s junit task Effective Testing

JUnit Java testing framework Written by Erich Gamma and Kent Beck Pass or Fail tests Open source http://junit.org hosted on SourceForge Tons of extensions Integrates with Ant!! junit task Released under the IBM's Common Public License Version 1.0 Optional task included in Ant’s (1.5.1) optional.jar file Effective Testing

Writing A JUnit Test public class TestCustomer extends junit.framework.TestCase { // define instance variables public void setUp() { // initialize test } public void tearDown() { // clean up test public void testXXX() { // assertions public void testYYY() { * Test methods should execute as quickly as possible * Why? As more tests are added to the test suite the longer it takes for ALL tests to run. Consequences? Developers may grow tired of waiting and quit running the tests. Effective Testing

What’s In A Name? Class names are personal preference Just be consistent Consistency facilitates automation Effective Testing

JUnit and Ant The junit task is contained in Ant’s optional JAR library (already on Ant’s classpath). <junit printsummary="on" fork="false" haltonfailure="false"> <classpath refid="classpath.project"/> <formatter type="xml"/> <batchtest fork="false" todir="${dir.build}"> <fileset dir="${dir.src}"> <include name="**/Test*.java"/> </fileset> </batchtest> </junit> Other types: brief and plain Consistent naming conventions serve two purposes: 1.) They make your code more maintainable 2.) Consistency facilitates automation Effective Testing

JUnit Pretty Printing junitreport <junitreport todir="${dir.build}"> <fileset dir="${dir.build}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${dir.build}"/> </junitreport> <exec executable="open" os="Mac OS X"> <arg value="${dir.build}/index.html"/> </exec> Effective Testing

JUnit Pretty Printing junitreport output ALWAYS NEEDS TO BE 100% Effective Testing

JUnit Pretty Printing junitreport output Effective Testing

Ant Diagram One command prepare compile jar junit If executing server side tests you’ll need to deploy first junit junitreport Depends On References exec User-defined Target Task Definition Effective Testing

Mock Objects Fake implementation of a class or interface Utilized in “advanced” unit testing Should only provide functionality needed for testing May provide self-validation You can refactor Mock Objects as more functionality is needed. Fail fast if unexpected or invalid state is detected. Collect results during execution and validate against expected results. Effective Testing

Mock Objects Two Schools Of Thought Any dummy object that stands in for a real object Has the ability to set expectations and provide self-validation http://www.mockobjects.com These guys are trying to create a central point of reference for all Mock Objects. Effective Testing

Data Access Layer (DAO) Why Use Mock Objects? Test difficult aspects of system Provide “fake” server layer Mock Factory is configured to return a Mock Implementation. Example: -Ddao.factory=mock Data Access Layer (DAO) Oracle Business Object MySQL Test Ask DAO Factory for mock object Set expectations on mock object - Invalid data throws exception - Data not found throws exception Invoke DAO method Assert expectations Files Effective Testing

Mock Object Frameworks Effective Testing

Testing Swing Code Swing relies on models and views Communicates using events Use Mock Objects to simulate event listeners Example: TableModels communicate using TableModelListeners Example: We want to test that models fire the correct events. The next few slides show this… Effective Testing

Testing Swing Code Example Diagram TableModelListener TableModelEvent valueChanged(TableModelEvent) implements TableModelEvent Fires Event JTable TableModel public void valueChanged(TableModelEvent) { // updates the view } Add Row Delete Row Swap Rows Effective Testing

MockTableModelListener Testing Swing Code Example Diagram TableModelListener valueChanged(TableModelEvent) implements TableModelEvent Fires Event MockTableModelListener TableModel public void valueChanged(TableModelEvent) { // see next slide } Add Row Delete Row Swap Rows Effective Testing

Testing Swing Code MockTableModelListener Mock objects typically perform assertions import junit.framework.Assert; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.TableModel; import java.util.ArrayList; import java.util.List; public class MockTableModelListener implements TableModelListener { private static final int NONE_EXPECTED = -1; private List events = new ArrayList(); private List expectedEvents = null; private int expectedEventCount = NONE_EXPECTED; public void tableChanged(TableModelEvent e) { this.events.add(e); if (this.expectedEventCount > NONE_EXPECTED && this.events.size() > this.expectedEventCount) { Assert.fail("Exceeded the expected event count: " + this.expectedEventCount); } Fail Fast! Effective Testing

Testing Swing Code MockTableModelListener public void addExpectedEvent(TableModelEvent e) { if (this.expectedEvents == null) { this.expectedEvents = new ArrayList(); } this.expectedEvents.add(new ComparableTableModelEvent(e)); public void setExpectedEventCount(int n) { this.expectedEventCount = n; public void verify() { if (this.expectedEventCount > NONE_EXPECTED) { Assert.assertEquals("Expected event count", this.expectedEventCount, this.events.size()); if (this.expectedEvents != null) { Assert.assertEquals("Expected events", this.expectedEvents, this.events); Effective Testing

Testing Swing Code MockTableModelListener public int getEventCount() { return this.events.size(); } public List getEvents() { return this.events; class ComparableTableModelEvent extends TableModelEvent { public ComparableTableModelEvent(TableModelEvent orig) { super((TableModel) orig.getSource(), orig.getFirstRow(), orig.getLastRow(), orig.getColumn(), orig.getType()); public boolean equals(Object obj) { TableModelEvent tm = (TableModelEvent) obj; return getSource() == tm.getSource() && getFirstRow() == tm.getFirstRow() && getLastRow() == tm.getLastRow() && getColumn() == tm.getColumn() && getType() == tm.getType(); Effective Testing

Testing Swing Code Using The MockTableModelListener public void testAddAccountFiresCorrectEvent() { MockTableModelListener mockListener = new MockTableModelListener(); mockListener.setExpectedEventCount(1); TableModelEvent evt = new TableModelEvent( this.acctTableModel, this.accounts.length, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT); mockListener.addExpectedEvent(evt); this.acctTableModel.addTableModelListener(mockListener); this.acctTableModel.addAccount( new Account(Account.CHECKING, "12345", 100.50)); mockListener.verify(); } Don’t forget to add the listener to the model. Effective Testing

Breaking Out Logic Refactoring If your GUI code contains business logic Create testable helper classes new JButton(new AbstractAction("Calculate") { public void actionPerformed(ActionEvent event) { // perform calculation int value = Integer.parseInt(valueField.getText()); double newValue = value * 100.0D; calculatedValue.setText(String.valueOf(newValue)); } }); We want to test our calculation, but it’s very hard because the UI components get in the way. public class CalculationHelper { public static double calculateSomething(int value) { return value * 100.0D; } Now test this class independently of Swing. Effective Testing

Swing Testing Pyramid Simulation JUnit JUnit Component Integration - java.awt.Robot - Abbot JUnit JUnit - Lots of Mock Event Listeners More mock objects Test state Test colors etc Events Components Effective Testing

Swing Possibilities Easy Difficult (but possible) Document Objects Graphics Shapes and Bounds (Rectangles) Swing Models Swing Actions Difficult (but possible) Focus User interactions Effective Testing

GUI Testing Frameworks Effective Testing

Testing Server Code Before resorting to server side testing Try to design/ refactor your code into standalone classes (testable with JUnit) Remove dependency for javax.ejb.* javax.servlet.* Ask yourself “Do I need a full-blown container?” “Could I use mock objects?” Effective Testing

Testing Server Code Avoid EJB Testing public class AccountBean implements SessionBean { // a bunch of EJB methods public void ejbCreate() { ... } public void ejbActivate() { ... } public void ejbPassivate() { ... } public double getBalanceAsOf(Date date) { // fetch data ... // apply interest // deduct bank fees } Way too hard to test this stuff. Break it out!! Effective Testing

Testing Server Code Avoid EJB Testing (better solution) public class AccountBean implements SessionBean { // a bunch of EJB methods public void ejbCreate() { ... } public void ejbActivate() { ... } public void ejbPassivate() { ... } etc... public double getBalanceAsOf(Date date) { // delegate to a business object return AccountBO.getBalanceAsOf(date); } Now we can test the AccountBO class outside of a container (just use JUnit) Effective Testing

Cactus Used for server side testing http://jakarta.apache.org/cactus Provides in-container testing Direct access to invoking methods on servlets, JSPs, filters, EJBs Executes on both the client and server Really hard to configure In my opinion! Of course, the book shows recipes on how to setup Cactus with Ant. ;-) Effective Testing

Cactus A Simple Test import org.apache.cactus.ServletTestCase; import org.apache.cactus.WebRequest; public class TestLoginServlet extends ServletTestCase { private LoginServlet servlet; public void setUp() { this.servlet = new LoginServlet(); } // executes on the client public void beginValidFormParameters(WebRequest webRequest) { webRequest.addParameter("username", "coyner_b", WebRequest.POST_METHOD); webRequest.addParameter("password", "secret", // executes on the server public void testValidFormParameters() { assertTrue("Valid Parameters.", this.servlet.validateParameters(this.request)); Effective Testing

HttpUnit Used to interact with HTTP servers Not a testing tool Uses JUnit, though Provides API for: Parsing HTML Submitting Form Data Following hyperlinks Effective Testing

HttpUnit A Simple Test A WebConversation is HttpUnit’s browser import com.meterware.httpunit.*; import junit.framework.TestCase; public class TestNewsletter extends TestCase { public void testIndexPageExists() throws Exception { // throws HttpNotFoundException if the URL is invalid new WebConversation(). getResponse("http://localhost:8080/news"); } A WebConversation is HttpUnit’s browser getResponse() opens a connection to the web server and requests the URL Effective Testing

Testing A Data Layer Dummy Data Access Objects Mock the SQL (JDBC stuff) Have a static DB Hard to keep a good state Here’s a cool idea: The folks at www.mockobjects.com provide a set of JDBC mock objects. If using MS Access you can create a populated DB (*.mdb file) for your tests. After each test just replace the file with the master file and you have a clean DB. Effective Testing

Summary Have a test-first mindset Create a stable build environment using Ant Break up code into testable classes Break up Swing code Break up server code Be patient, testing is an art Effective Testing

References Java Extreme Programming Cookbook Eric M. Burke and Brian M. Coyner O’Reilly http://www.oreilly.com/catalog/jextprockbk/ Available March 2003 Information contained in this presentation is courtesy of O’Reilly’s Java Extreme Programming Cookbook book. Effective Testing