21
Can you test database layer more smart than usual? Serhii Kartashov December 2012 SoftServe

Testing database content with DBUnit. My experience

Embed Size (px)

Citation preview

Page 1: Testing database content with DBUnit. My experience

Can you test database layer more smart than usual?

Serhii KartashovDecember 2012SoftServe

Page 2: Testing database content with DBUnit. My experience

Agenda

What’s a problem?

How we want to resolve that?

Are you professional?

Page 3: Testing database content with DBUnit. My experience

Agenda

What’s a problem?

How we want to resolve that?

Are you professional?

Page 4: Testing database content with DBUnit. My experience

Problem

Task:

please remove old and didn’t marked data from production!

The problem is this:

you have a SQL database, some stored procedures, and a layer of code sitting between your application and the database. How can you put tests in place to make sure your code really is reading and writing the right data from the database?

Page 5: Testing database content with DBUnit. My experience

What’s the best practice? You need [multiple] databases!

Aster

SQL Oracle

MySQL

Aster

Oracle

MySQL

Aster

Oracle

MySQL

devQA (local) prodQA production

Page 6: Testing database content with DBUnit. My experience

How I wanted to do that? First step: initialization test data.

con.

Prepare the data

object file network

SQL commit

Page 7: Testing database content with DBUnit. My experience

First step: create the Test and call API.

Page 8: Testing database content with DBUnit. My experience

How I wanted to do that? First step: initialization test data.

con

.

Actual Data

DB

SQL

Expected Data

Compare

Page 9: Testing database content with DBUnit. My experience

Agenda

What’s a problem?

How we want to resolve that?

Are you professional?

Page 10: Testing database content with DBUnit. My experience

Ideal schema

DB DBDB

Initialize call API Actual dataExpected

data

Compare

Step 1 Step 2 Step 3

File

File

Page 11: Testing database content with DBUnit. My experience

Agenda

What’s a problem?

How we want to resolve that?

Are you professional?

Page 12: Testing database content with DBUnit. My experience

DBUnit Framework

Page 13: Testing database content with DBUnit. My experience

DBUnit Core Components

• IDatabaseConnection - interface representing a DbUnitconnection 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. Operations:

• NONE• UPDATE• INSERT• REFRESH• DELETE• DELETE_ALL• TRUNCATE_TABLE• CLEAN_INSERT (DELETE_ALL and INSERT)

Page 14: Testing database content with DBUnit. My experience

IDatabaseConnection

Page 15: Testing database content with DBUnit. My experience

IDataSet: database

DataBase

client_id item_id item_name

analytics_comments

client_id item_id item_name

200 01 name - #1

200 02 name - #2

200 03 name - #3

Page 16: Testing database content with DBUnit. My experience

IDataSet: XML file Initialized

Expected

Page 17: Testing database content with DBUnit. My experience

Database Operation: setUp()

Page 18: Testing database content with DBUnit. My experience

Simple JUnit test

Page 19: Testing database content with DBUnit. My experience

A bit more

Page 20: Testing database content with DBUnit. My experience

Supported RDBMS

• OracleDatabase• MsSQL• MySQL• IbmDB2• IbmInformix• H2• HypersonicSQL• PostgreSQL• SybaseSQL• InterBase• OpenBase8• SapDB/MaxDB• Derby• FrontBase

Where is Aster database???

NOTE: 1. latest version of DBUnit framework doesn’t support Oracle Schemas…2. and very inert community…

Page 21: Testing database content with DBUnit. My experience

Thank you!

Questions?