Presentation is loading. Please wait.

Presentation is loading. Please wait.

Testing a persistence layer

Similar presentations


Presentation on theme: "Testing a persistence layer"— Presentation transcript:

1 Testing a persistence layer
Chris Richardson 1/10/2019 Copyright (c) Chris Richardson. All rights reserved.

2 Copyright (c) 2006 Chris Richardson. All rights reserved.
About Chris… Grew up in England Live in Oakland, CA Twenty years of software development experience Building object-oriented software since 1986 Developing with Java since 1996 Using J2EE since 1999 Author of POJOs in Action Run a consulting company that helps organizations build better software faster Chair of the eBIG Java SIG ( 1/10/2019 Copyright (c) Chris Richardson. All rights reserved.

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

4 The challenge of testing
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/10/2019 Copyright (c) Chris Richardson. All rights reserved.

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

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

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

8 Pattern #2: Test the O/R mapping
Problem: 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: Read XML or metadata Make assertions about the mapping 1/10/2019 Copyright (c) Chris Richardson. All rights reserved.

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

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

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

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

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

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

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

16 Copyright (c) 2006 Chris Richardson. All rights reserved.
For more information Buy my book Stop by Terracotta’s booth (#408) Send Visit my website Book Blog This presentation ORMUnit 1/10/2019 Copyright (c) Chris Richardson. All rights reserved.


Download ppt "Testing a persistence layer"

Similar presentations


Ads by Google