Joe Borosky Chris DeCelles Yvonne Krashkevich

Slides:



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

Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Approach of Unit testing with the help of JUnit Satish Mishra
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
© 2012 LogiGear Corporation. All Rights Reserved Robot framework.
Data File Access API : Under the Hood Simon Horwith CTO Etrilogy Ltd.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
DbUnit Framework Joe Borosky Chris DeCelles Yvonne Krashkevich.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Dwight Deugo Nesa Matic
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
ADO .NET from. ADO .NET from “ADO .Net” Evolution/History of ADO.NET MICROSOFT .NET “ADO .Net” Evolution/History of ADO.NET History: Most applications.
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.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
ASP.NET Programming with C# and SQL Server First Edition
DEPTT. OF COMP. SC & APPLICATIONS
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
PLM, Document and Workflow Management
Web Technologies IT230 Dr Mohamed Habib.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Data Virtualization Demoette… ADO.NET Client
LOCO Extract – Transform - Load
Introduction to JUnit CS 4501 / 6501 Software Testing
Data Virtualization Tutorial… OAuth Example using Google Sheets
Unit testing Java programs Using JUnit
IS444: Modern tools for applications development
Brian Leonard ブライアン レオナルド
JDBC.
IS444: Modern tools for applications development
An Automated Testing Framework
Tutorial 1 – Introduction To Microsoft Access 2003
What’s changed in the Shibboleth 1.2 Origin
Tutorial 1 – Introduction To Microsoft Access 2003
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
Developing a Model-View-Controller Component for Joomla Part 3
Introduction to JUnit IT323 – Software Engineering II
CS 240 – Advanced Programming Concepts
Developing and testing enterprise Java applications
Joel Adams and Jeremy Frens Calvin College
Exploring Microsoft® Office 2016 Series Editor Mary Anne Poatsy
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Plug-In Architecture Pattern
Presentation transcript:

Joe Borosky Chris DeCelles Yvonne Krashkevich DbUnit Framework Joe Borosky Chris DeCelles Yvonne Krashkevich

DbUnit Framework Database Testing with DbUnit The Environment In this presentation, we are going to explain how to get started with DbUnit. The Environment Sample Eclipse project which contains DbUnit and JUnit.

DbUnit Framework DbUnit extends the popular JUnit test framework. JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by those who implement unit tests in Java. It is open source software. It is an instance of the xUnit architecture for unit testing frameworks.

DbUnit Framework JUnit Features Assertions for testing expected results Text fixtures for sharing common test data Test suites for easily organizing and running tests Graphical and textual test runners

DbUnit Framework Unit Test Frameworks Unit Test Frameworks are a key element in eXtreme Programming and Agile Development. Unit Testing has moved beyond eXtreme Programming It is common in many types of application development. They help ensure low level code correctness, reduce software development cycle time, improve developer productivity, and produce more robust software.

DbUnit Best Practices DBUnit Best Practices Use one database instance per developer If you setup the tests properly you do not have to cleanup afterwards. Use multiple small datasets instead of using one large dataset (most tests do not require the entire database to re-initialize). Perform setup of stale data once for the entire test class or test suite Connection Management Strategies Remote Client with Database Test Case In-container with Cactus or JUnitEE

DbUnit Framework Database Testing with DbUnit DbUnit is a useful and powerful tool for simplifying unit testing of database operations. It is targeted for Database-driven projects and works to put your database into a known state between test runs. With DbUnit, a database can be seeded with a desired data set before a test; moreover, at the completion of the test, the database can be placed back into its pre-test state.

DbUnit Framework Database Testing with DbUnit It is a great tool to use to avoid the problems that may occur when one test case corrupts the database and causes subsequent test to fail. It can work with very large datasets when used in streaming mode. It can help verify your data matches an expected set of values.

DbUnit Framework DbUnit Features for Unit Testing Database Operations a very simple XML-based mechanism for loading test data a framework which simplifies operations for each stage in the life cycle of individual database tests an equally simple mechanism for exporting existing test data into the XML format for subsequent use in automated tests methods for comparing data, between flat files, queries and database tables

DbUnit Framework DbUnit Core Components IDatabaseConnection - interface representing a DbUnit connection to a DB IDataSet - interface representing a collection of tables DatabaseOperation - abstract class representing an operation performed on the database before and after each test

DbUnit Framework DbUnit IDatabaseConnection DatabaseConnection - wraps a JDBC connection DatabaseDataSourceConnection - wraps a JDBC Data Source

DbUnit Framework DbUnit IDataSet This is the primary abstraction used by DbUnit to manipulate tabular data. Commonly used implementations FlatXmlDataSet DefaultDataSet XmlDataSet CompositeDataSet StreamingDataSet FilteredDataSet DatabaseDataSet XlsDataSet QueryDataSet ReplacementDataSet

DbUnit Framework DbUnit DatabaseOperation Operations (named “DatabaseOperation.”xyz”) UPDATE TRUNCATE INSERT REFRESH DELETE CLEAN_INSERT DELETE_ALL NONE Other Operations CompositeOperation TransactionOperation IdentityInsertOperation

DbUnit Framework Create a DbUnit Test Case The easiest way to understand how DbUnit is used is through an example. The first thing to know is Instead of subclassing JUnit's TestCase class directly Subclass DbUnit's DatabaseTestCase, which is itself a subclass of TestCase. DatabaseTestCase is abstract, and requires you to implement two methods: protected IDatabaseConnection getConnection() throws Exception protected IDataSet getDataSet() throws Exception.

DbUnit Framework Create a DbUnit Test Case IDatabaseConnection getConnection() throws Exception. The first thing that needed is to specify how to obtain a database connection. protected IDatabaseConnection getConnection() throws Exception { Class driverClass = Class.forName("com.mysql.jdbc.Driver"); Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Framework Database", "root", “password"); return new DatabaseConnection(jdbcConnection); }

DbUnit Framework Create a DbUnit Test Case IDataSet getDataSet() throws Exception. Here the method returns a data set loaded from an XML file on the classpath. protected IDataSet getDataSet() throws Exception { loadedDataSet = new FlatXmlDataSet( this.getClass().getClassLoader().getResourceAsStream("input.xml")); return loadedDataSet; }

DbUnit Framework Create a DbUnit Test Case DbUnit has adopted a very flexible architecture based on intelligent use of interfaces. IDataSet is one of the key interfaces. The best way to think of IDataSet is that it represents one or more blocks of tabular data. This data may be generated from a number of sources: flat XML files, of which we will see an example very shortly tables in the database database queries created using SQL less obvious sources, such as Microsoft Excel spreadsheets

DbUnit Framework Create a DbUnit Test Case The IDataSet interface is very powerful First because it defines a common representation which can be used for comparisons between data from any of these sources Second because it defines methods for reading from and writing to its native format.

DbUnit Framework Create a DbUnit Test Case Probably the most widely used mechanism for creating a test data set is the flat XML file format. <?xml version='1.0' encoding='UTF-8'?> <dataset> <MANUFACTURER FIELD_1="1" FIELD_2="IBM" FIELD_3="Laptop" FIELD_4="2005-10-08" FILED_5="00:00:00"/> <MANUFACTURER FIELD_1="2" FIELD_2="DELL" FIELD_3="Desktop" FIELD_4="2005-10-09" FILED_5="00:00:00"/> </dataset>

DbUnit Framework Create a DbUnit Test Case The test case contains data from a single table, MANUFACTURER, with five data columns. Our getDataSet() implementation uses the FlatXmlDataSet class to load this file. The test class is not ready, because we haven't written any tests yet. Before we can do this, we need to understand the DbUnit test life cycle. In particular, we want to know what it does with the IDataSet it has loaded in the getDataSet() method.

DbUnit Framework The DbUnit Test LifeCycle The most typical scenario for running database tests using DbUnit is as follows: remove any old stale data left in the database from previous tests load some data from the file system into the database run some tests, using DbUnit methods to make assertions

DbUnit Framework The DbUnit Test LifeCycle Both of the first two operations occur during execution of the JUnit setUp() method. Note that with this scenario, no attempt is made to restore database state after running tests. This is the recommended best practice by the authors of DbUnit.

DbUnit Framework The DbUnit Test LifeCycle Cleanup occurs before the test is run, not after. This approach is more productive when circumstances allow it, because you do not need to waste time writing code to restore state, which can be quite a significant task in some situations. The tests may run a little slower, but the idea is that setup should only load data required for an individual test, rather than data required for all tests.

DbUnit Framework The DbUnit Test LifeCycle Lets take a look at how the setUp() and tearDown() methods are implemented in the DatabaseTestCase class.

DbUnit Framework protected void setUp() throws Exception { super.setUp(); executeOperation(getSetUpOperation()); } protected void tearDown() throws Exception super.tearDown(); executeOperation(getTearDownOperation());

DbUnit Framework /** * Returns the database operation executed in test setup. */ protected DatabaseOperation getSetUpOperation() throws Exception { return DatabaseOperation.CLEAN_INSERT; } * Returns the database operation executed in test cleanup. protected DatabaseOperation getTearDownOperation() throws Exception return DatabaseOperation.NONE;

DbUnit Framework The DbUnit Test LifeCycle The setUp() method executes the setup operation, DatabaseOperation.CLEAN_INSERT. This operation in fact combines two operations: DELETE_ALL and INSERT. DELETE_ALL removes all data from the database for the tables returned using getDataSet(), then inserts the data loaded using that method. This is how the data that we define in our flat XML file is loaded into the database.

DbUnit Framework The DbUnit Test LifeCycle The operation NONE executed in tearDown(), does nothing. The default setUp() and tearDown() operations can be overridden if your application requires this. You can even provide your own implementation of IDatabaseOperation.

DbUnit Framework Writing Tests Now that we understand the DbUnit lifecycle and we've set up the necessary infrastructure, we're ready to write some tests. The following tests demonstrate some features of DbUnit, rather than testing a specific application. First Test Example - Demonstrates that the setUp() method does indeed load the XML data into an IDataSet instance, and that this data contains the data we expect.

DbUnit Framework private FlatXmlDataSet loadedDataSet; protected IDataSet getDataSet() throws Exception { loadedDataSet = new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("input.xml")); return loadedDataSet; } public void testCheckDataLoaded() throws Exception assertNotNull(loadedDataSet); int rowCount = loadedDataSet.getTable(TABLE_NAME).getRowCount(); assertEquals(2, rowCount);

DbUnit Framework Writing Tests Second Test Example - Shows how we can create an IDataSet using the IDatabaseConnection for a particular table. The IDatabaseConnection is created using the IDatabaseConnection getConnection(). Notice that this is only using one table, although the IDataSet could contain data for multiple tables.

DbUnit Framework public void testCompareDataSet() throws Exception { IDataSet createdDataSet = getConnection().createDataSet(new String[] TABLE_NAME }); Assertion.assertEquals(loadedDataSet, createdDataSet); }

DbUnit Framework Writing Tests We mentioned earlier that DbUnit included a mechanism for comparing data loaded from different sources. You can see this at work in this test example: the Assertion.assertEquals() method compares data obtained from the database with the data loaded from the XML file.

DbUnit Framework Writing Tests Third Test Example - Takes this idea further. It shows how we can create an IDataSet instance using SQL queries, and use this as a basis for assertions

DbUnit Framework public void testCompareQuery() throws Exception { QueryDataSet queryDataSet = new queryDataSet(getConnection()); queryDataSet.addTable(TABLE_NAME, "SELECT * FROM " + TABLE_NAME); Assertion.assertEquals(loadedDataSet, queryDataSet); }

DbUnit Framework This is very powerful because it means you can create data sets which represent arbitrary queries, and not simply tables in the database. Many of the DbUnit test that will be written in real application are likely to use this mechanism.

DbUnit Framework Writing Tests Final Test Example - Shows how to export a data set, either from an existing database table or from a query, into a flat XML file. The FlatXmlDataSet.write() method is used to write the data set to the file output.xml in the DbUnit XML format.

DbUnit Framework public void testExportData() throws Exception { IDataSet dataSet = getConnection().createDataSet(new String[] TABLE_NAME }); URL url = DatabaseTestCase.class.getResource("/input.xml"); assertNotNull(url); File inputFile = new File(url.getPath()); File outputFile = new File(inputFile.getParent(), "output.xml"); FlatXmlDataSet.write(dataSet, new FileOutputStream(outputFile)); assertEquals(FileUtils.readFileToString(inputFile, "UTF8"), FileUtils.readFileToString(outputFile, "UTF8")); }

DbUnit Framework Writing Tests Our test also asserts that the contents of the outputted XML file are identical to the input.xml from which the IDataSet was originally loaded.

DbUnit Framework Summary DbUnit is a powerful, simple to use tool for enabling test driven development of database applications. It is well designed around flexible Java interfaces, which make it easy to adapt or extend to application requirements. It is simple to include DbUnit as a part of your database application development process and take advantage of its powerful, timesaving features.

DbUnit Framework Summary The DbUnit framework's ability to manage the state of a database throughout a test's lifecycle enables rapid test-case creation and adoption; furthermore, by controlling a major dependency, tests that utilize the DbUnit framework are easily automated. DbUnit's elegant design makes learning how to properly utilize its features a breeze. Once it's in place as a part of an effective testing strategy, overall code stability will increase dramatically, along with the collective confidence of your development team.

DbUnit Framework QUESTIONS?

DbUnit Framework Sources: http://www.realsolve.co.uk/site/tech/dbunit-quickstart.php http://www.onjava.com/pub/a/onjava/2004/01/21/dbunit.html http://dbunit.sourceforge.net/ http://www.dbunit.org/apidocs/index.html http://www.eclipse.org/ http://perl.about.com/od/installandconfiguremysql/l/aa112400b.htm http://dev.mysql.com/doc/mysql/en/create-database.html