Testing a persistence layer Chris Richardson chris chrisrichardson

  • Slides: 16
Download presentation
Testing a persistence layer Chris Richardson chris@ chrisrichardson. net http: //www. chrisrichardson. net 1/8/2022

Testing a persistence layer Chris Richardson chris@ chrisrichardson. net http: //www. chrisrichardson. net 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 1

About Chris… o o o Grew up in England Live in Oakland, CA Twenty

About Chris… o o o Grew up in England Live in Oakland, CA Twenty years of software development experience n n n o o o 1/8/2022 Building object-oriented software since 1986 Developing with Java since 1996 Using J 2 EE since 1999 Author of POJOs in Action Run a consulting company that helps organizations build better software faster Chair of the e. BIG Java SIG ( www. ebig. org ) Copyright (c) 2006 Chris Richardson. All rights reserved. 2

What’s a persistence layer? o SQL-based DAOs : n n o Execute SQL statements

What’s a persistence layer? o SQL-based DAOs : n n o Execute SQL statements Use JDBC, i. BATIS ORM-based DAOs : n n 1/8/2022 Use Hibernate, JDO or JPA O/R mapping defined by annotations or XML Copyright (c) 2006 Chris Richardson. All rights reserved. 3

The challenge of testing o o o Testing is like flossing: essential to prevent

The challenge of testing o o o Testing is like flossing: essential to prevent software decay But some of us find it difficult to do regularly You need lots of tests You want the tests to run quickly But databases are slow Writing database setup code can be difficult 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 4

Testing strategies o Test with mocks (fast) n n o Test the O/R mapping

Testing strategies o Test with mocks (fast) n n o Test the O/R mapping metadata (fast) n o o Test the business logic with mock DAOs Test the DAOs with mock ORM APIs Verify that classes are mapped correctly Write some database tests (slow but fewer) n Test persistent object CRUD n Test the queries Whenever you can: n Use an in-process/memory DB such as HSQLDB n Don’t commit transactions 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 5

Pattern #0: Mock the persistent layer o Goal: n o But tests that use

Pattern #0: Mock the persistent layer o Goal: n o But tests that use the DB are: n n o Slow running Difficult to write How: n n o Test the business logic Use fast, easy to write, in-memory tests Use mocks for the DAOs Works nicely with dependency injection 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 6

Pattern #1: Mock the ORM framework o o DAO = class that wraps the

Pattern #1: Mock the ORM framework o o DAO = class that wraps the ORM framework Problem: n n o Bugs in the logic of the DAOs Testing against the DB is slow Solution n 1/8/2022 Use mock objects for the ORMF APIs Copyright (c) 2006 Chris Richardson. All rights reserved. 7

Pattern #2: Test the O/R mapping o Problem: n n o Incorrectly defined mapping,

Pattern #2: Test the O/R mapping o Problem: n n o Incorrectly defined mapping, e. g. forgetting to map a field is a common bug But tests that save objects and check the contents of the DB are slow to execute and tedious to write Solution: n n 1/8/2022 Read XML or metadata Make assertions about the mapping Copyright (c) 2006 Chris Richardson. All rights reserved. 8

Pattern #3: Test the schema o Problem: n n o Solutions: n n o

Pattern #3: Test the schema o Problem: n n o Solutions: n n o Object/relational mapping can reference non-existent tables and columns Only some execution paths will access those tables and columns Have the ORM validate the schema Write a test that verifies that all referenced tables and columns exists How n 1/8/2022 Persistence frameworkspecific Copyright (c) 2006 Chris Richardson. All rights reserved. 9

Pattern #4: CRUD tests for the persistent objects o Problem: n o Solution n

Pattern #4: CRUD tests for the persistent objects o Problem: n o Solution n o Verify that the objects can be created, updated and deleted Each test takes an object through its lifecycle n n n o Database constraints can prevent application from updating the database Creates and saves an object One or more times: Load and update it Delete the object These tests are slow(er) 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 10

Pattern #5: Test the queries o Problem: n n o Solution: n o Write

Pattern #5: Test the queries o Problem: n n o Solution: n o Write tests for the queries Each test: n n n o Malformed queries Logic errors in queries, e. g. < instead of <= Populates database with test objects Execute queries with parameters Verify that the query returns the expected result These tests can be slow 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 11

Use an in-process/in-memory database o o For example: HSQLDB Benefits: n o Much faster

Use an in-process/in-memory database o o For example: HSQLDB Benefits: n o Much faster than a traditional DB: committing transactions, creating schema Drawbacks: n n 1/8/2022 Different SQL unless using ORM Some incompatibilities: E. g. time precision Copyright (c) 2006 Chris Richardson. All rights reserved. 12

Committing transactions or not o Strategy n n o Benefits: n n o Execute

Committing transactions or not o Strategy n n o Benefits: n n o Execute test in a transaction Rollback transaction at the end Leaves the database unchanged Faster Drawbacks: n n 1/8/2022 Commit time constraints not checked Services calls that execute in a different transaction can’t see the changes Copyright (c) 2006 Chris Richardson. All rights reserved. 13

ORMUnit – JUnit extension o o Simplifies persistence layer tests Status: n n n

ORMUnit – JUnit extension o o Simplifies persistence layer tests Status: n n n 1/8/2022 Supports Hibernate and JDO Currently, part of book’s source code But will be separated out Copyright (c) 2006 Chris soon Richardson. All rights reserved. 14

Summary o o o Write lots of tests Test the metadata when possible Avoid

Summary o o o Write lots of tests Test the metadata when possible Avoid the DB whenever you can 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 15

For more information o Buy my book o Stop by Terracotta’s booth (#408) Send

For more information o Buy my book o Stop by Terracotta’s booth (#408) Send email: chris@chrisrichardson. net o o Visit my website n n Book Blog This presentation ORMUnit http: // www. chrisrichardson. net 1/8/2022 Copyright (c) 2006 Chris Richardson. All rights reserved. 16