let the continuous build embrace your database

36
Continuous Database Build And: Practical Approaches For DB Based Functional Testing Stefan Rufer – Netcetera AG Michael Wernli – Telekurs Card Solutions AG 3020

Upload: cedricwalter

Post on 14-Dec-2014

852 views

Category:

Documents


3 download

DESCRIPTION

JUnit tests should not depend on database state." - "Set up your test data before you run your test." - We figure this just does not always scale. Mocking test data for hundreds of tables may not be suitable and database schemes evolve as the application does. These are common challenges when developing large J2EE systems. This presentation shows practical approaches for project setups and (functional) tests that decided to depend on a database. Developers and build servers may or may not share the database and support for this should be as simple as possible. We give an overview of what proved to be a good setup for an Eclipse / Maven 2 based development and build environment that relies on an evolving relational schema.

TRANSCRIPT

Page 1: Let the Continuous Build Embrace Your Database

Continuous Database BuildAnd: Practical Approaches For DB Based Functional Testing

Stefan Rufer – Netcetera AG

Michael Wernli – Telekurs Card Solutions AG

3020

Page 2: Let the Continuous Build Embrace Your Database

2

Speakers

> Stefan Rufer

– Studied business IT at the University of Applied Sciences in

Bern

– Senior Software Engineer and Architect at Netcetera

– Main interest: Server side applicat ion development using JEE

> Michael Wernli

– Cert if ied engineer in IT

– Senior Software Engineer at Telekurs Card Solut ions

– As Applicat ion DBA in charge of large- scale cashless payment

t ransact ion system.

Page 3: Let the Continuous Build Embrace Your Database

3

Why are we here?

> You will see how a cont inuous database build improved our

overall build stability and helped us to focus on business

problems during integrat ion tests.

Page 4: Let the Continuous Build Embrace Your Database

4

Motivation

> “Who the #@!* dropped the salary column in the build database?!”

– Dude, you didn‘t run the script to add it in the f irst place…

> Did the build just turn red…

– because someone forgot to clean up the build database –

AGAIN?

> svn revert drop-that-table.sql

– Hmm, the table is st ill missing in the DB…?

> ……

Page 5: Let the Continuous Build Embrace Your Database

5

Motivation

Page 6: Let the Continuous Build Embrace Your Database

6

AGENDA

> Continuous Build For The Database

> Re- Runnable Functional JUnit Tests

> Eclipse/ Maven Setup For Functional JUnit Tests

> Outlook / References

Page 7: Let the Continuous Build Embrace Your Database

7

> Continuous Build For The Database

> Re- Runnable Functional JUnit Tests

> Eclipse/ Maven Setup For Functional JUnit Tests

> Outlook / References

Page 8: Let the Continuous Build Embrace Your Database

8

The Problem

> 100..200 SQL scripts (DDL+ DML) per major release

> Scripts have to be runnable f irst - to- last at all t imes

> 20 developer/ build/ integrat ion databases to keep in sync

Poor Database Administrator!

Page 9: Let the Continuous Build Embrace Your Database

9

The Idea

> Install database scripts as soon as they are commited to the

subversion repository.

> Run SQL script against reference database. If installat ion

terminates without error, populate to all other databases.

> Store in the database what script was installed in what subversion

(SVN) revision.

Page 10: Let the Continuous Build Embrace Your Database

10

Basic Setup of a Continuous Database Build

Page 11: Let the Continuous Build Embrace Your Database

11

Demo Part I

Page 12: Let the Continuous Build Embrace Your Database

12

Different behaviour of database sources and Java sources during build

Page 13: Let the Continuous Build Embrace Your Database

13

Demo Part II

Page 14: Let the Continuous Build Embrace Your Database

14

We need re- runnable (idempotent) database source scripts

Page 15: Let the Continuous Build Embrace Your Database

15

Delta and Source Scripts

> Most dif f icult is data definit ion

or data modif icat ion scripts.

– delta-scripts: „run once“

– Add idempotency manually

> Packages, t riggers, views can

be reinstalled easily.

– source-scripts: „run

however“

– Idempotent by design

Page 16: Let the Continuous Build Embrace Your Database

16

Branching for database source scripts is a bit different

Page 17: Let the Continuous Build Embrace Your Database

17

Demo Part III

Page 18: Let the Continuous Build Embrace Your Database

18

Quality Checks

> Automated quality checks for

– Primary key constraint names

– Unique key constraint names

– Foreign key constraint names

– Check constraint names

– Index names for all of the above

– Sequence names

> Hooks provided to start quality

checks

Page 19: Let the Continuous Build Embrace Your Database

19

> Continuous Build For The Database

> Re- Runnable Functional JUnit Tests

> Eclipse/ Maven Setup For Functional JUnit Tests

> Outlook / References

Page 20: Let the Continuous Build Embrace Your Database

20

JUnit Tests Modifying Database

Page 21: Let the Continuous Build Embrace Your Database

21

How This All Relates

> JUnit tests that do insert / update/ delete must not leave their

t races in the build database. Auto rollback JUnit class

> Good experience by providing personal database to each

developer.

– But: How to keep it up to date? Cont inuous Database Build.

> Developers want to switch between a personal and a build

database. Eclipse/ Maven Setup

Page 22: Let the Continuous Build Embrace Your Database

22

The Idea

> AutoRollbackTestCase (extends JUnits class TestCase)

> Datasource and JDBC connect ion managed internally

> Rollback called during tearDown method of test no t races!

Page 23: Let the Continuous Build Embrace Your Database

23

Undo, Undo!

class ExampleDBTestCase extends AutoRollbackTestCase {

public void testDbInsert() throws SQLException { Statement stmt = getDataSource().getConnection().createStatement();

stmt.executeUpdate("INSERT INTO testtable values ('X')");

ResultSet rs = stmt.executeQuery("SELECT * FROM testtable"); assertTrue("expect one row", rs.next()); assertEquals("X", rs.getString("testrow")); assertFalse("expect only one row", rs.next()); }

}

Page 24: Let the Continuous Build Embrace Your Database

24

Little Burden – Big Deal

> JDBC insert / update/ delete statements are a crucial part of

enterprise applicat ions. They need to be tested!

> Avoid the burden of cleaning up the database (someone will

forget anyway…)

> Unit tests that roll back at the end offer a simple way to run SQL:

– Verify correct SQL syntax

– Verify database state after update

> Spring offers the possibilit ies of AutoRollbackTestCase (and much

more), check out spring-test.jar /

org.springframework.test

Page 25: Let the Continuous Build Embrace Your Database

25

> Continuous Build For The Database

> Re- Runnable Functional JUnit Tests

> Eclipse/ Maven Setup For Functional JUnit Tests

> Outlook / References

Page 26: Let the Continuous Build Embrace Your Database

26

Properties Overloading

> ConfiguredTestCase reads junit .propert ies

> If junit_fritz.propert ies is available for user „fritz“, it is read as

well

Overwrites propert ies in junit .propert ies

> How does this look like?

user=buildpassword=buildurl=jdbc:derby:builddb

example=buildsrv:80

user=integrationpassword=integration

example=localhost:8080

reuse URL

junit.properties junit_fritz.properties

Page 27: Let the Continuous Build Embrace Your Database

27

KISS – But How?

> Scope: Mult imodule build setup using Maven 2 and Eclipse

> Eclipse and Maven classpath handling dif ferent

> Eclipse: Mult iple projects referencing each other results in a

„global“ classpath.

Where to place database connect ion informat ion for unit tests?

> Maven: Each module needs junit .propert ies in

src/test/resources if we want to avoid test resources in the

main source t ree.

Where to place database connect ion informat ion for unit tests?

Page 28: Let the Continuous Build Embrace Your Database

28

Eclipse Setup For Functional JUnit Tests

> User adds his one and only junit_USERNAME.propert ies f ile to

„toplevel“ project

> Eclipse uses this for all JUnit runs as it is always on the classpath

User specif ic database connect ion set t ings in Eclipse.

Page 29: Let the Continuous Build Embrace Your Database

29

Maven Setup For Functional JUnit Tests

> junit .propert ies f ile in each submodule

> junit .propert ies contains variables:

> Values def ined in Maven POM…

…and injected during Maven lifecycle process-test-resources

db.user=${db.user}db.password=${db.password}db.schema=${db.schema}db.url=${db.url}

<db.user>build</db.user> <db.password>build</db.password><db.schema>build</db.schema><db.url>dpas10</db.url>

junit.properties

pom.xml

Page 30: Let the Continuous Build Embrace Your Database

30

Test- properties: Define once, use everywhere

Page 31: Let the Continuous Build Embrace Your Database

31

> Continuous Build For The Database

> Re- Runnable Functional JUnit Tests

> Eclipse/ Maven Setup For Functional JUnit Tests

> Outlook / References

Page 32: Let the Continuous Build Embrace Your Database

32

Outlook

> Continuous Database Script Suite current ly tailored to Oracle

systems

– Consider generic implementat ion or at least ident ify and

isolate specialized points

> Test data management is the next big topic on our radar. As we

have to select sensible stuff f rom 1TB of data we need some

clever ideas…

> Integrate Maven/ Eclipse setup with a tool like MavenIDE.

Page 33: Let the Continuous Build Embrace Your Database

33

Links / References

> Full source for continuous database build system

http:/ / www.stefanrufer.ch/ dbbuild

> Spring Test inghttp:/ / stat ic.springframework.org/ spring/ docs/ 2.5.x / reference/ test ing.html

Page 34: Let the Continuous Build Embrace Your Database

34

The End

Page 35: Let the Continuous Build Embrace Your Database

35

Two things to remember

Store in the database how the database was

built.

Write JUnit tests that roll back.

Page 36: Let the Continuous Build Embrace Your Database

Stefan Rufer [email protected]

Netcetera AG www.netcetera.ch

Michael Wernli [email protected]

Telekurs Card Solutions www.telekurs.com