Presentation is loading. Please wait.

Presentation is loading. Please wait.

Developing and testing enterprise Java applications

Similar presentations


Presentation on theme: "Developing and testing enterprise Java applications"— Presentation transcript:

1 Developing and testing enterprise Java applications
Chris Richardson 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

2 Copyright (c) 2005 Chris Richardson. All rights reserved.
Who am I? Twenty years of software development experience Building object-oriented software since 1986 Developing with Java since 1996 Author of POJOs in Action Run a consulting company that helps organizations develop software more effectively 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

3 Copyright (c) 2005 Chris Richardson. All rights reserved.
Key points EJBs are (mostly) a bad idea Plain Old Java Objects and lightweight frameworks make development easier and faster Don’t access the DB if you want thorough tests that execute quickly 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

4 Copyright (c) 2005 Chris Richardson. All rights reserved.
Agenda Overview of enterprise Java Developing with POJOs and lightweight frameworks Effective testing strategies 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

5 Copyright (c) 2005 Chris Richardson. All rights reserved.
J2EE architecture Client Server From J2EE spec 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

6 Copyright (c) 2005 Chris Richardson. All rights reserved.
EJB strengths It’s the standard for building object-oriented, distributed applications in Java Declarative transactions and security Truly distributed applications where EJBs participate in transactions initiated by a remote client Messaging-oriented applications that can benefit from message-driven beans 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

7 Copyright (c) 2005 Chris Richardson. All rights reserved.
EJBs weaknesses But most applications don’t need distributed transactions initiated by a remote client Code is coupled to the server container Long edit-compile-debug cycles Slow development and testing Excessive complexity Complicated XML descriptor files Complicated IDE requirements EJB encourage procedural programming and make object-oriented programming difficult Broken object/relational mapping EJB 3 fixes some things but … 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

8 Copyright (c) 2005 Chris Richardson. All rights reserved.
Agenda Overview of enterprise Java Developing with POJOs and lightweight frameworks Effective testing strategies 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

9 The solution: POJOs = Plain Old Java Objects
Java objects that don't implement any special interfaces Coined by Fowler to make it sound just as exciting as JavaBeans, Enterprise JavaBeans 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

10 Simple idea with surprising benefits
Simpler development Test without an application server Business logic and persistence are separate Faster development Test without deploying Easier testing More maintainable Modular object-oriented code Loosely coupled design Decouple technologies from core business logic 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

11 Copyright (c) 2005 Chris Richardson. All rights reserved.
Persisting POJOs  Object/relational mapping framework Hibernate Very popular open-source project JDO Standard from Sun – JSR 12 and JSR 243 Multiple implementations: Kodo JDO, Versant, JPOX EJB 3/JSR 220 persistence Intended to be “the” standard Java persistence mechanism But it has limitations, … 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

12 ORM framework features
Declarative mapping CRUD API Query language Transaction management Lazy and eager loading Caching Detached objects 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

13 Making POJOs transactional
An easy to use solution is the Spring framework Popular open-source framework for simplifying J2EE development Lightweight container for POJOs Provides declarative transactions for POJOs Makes it easier to use JDO and Hibernate 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

14 Spring lightweight container
Lightweight container = sophisticated factory for creating objects Spring bean = object created and managed by Spring You write XML that specifies how to create and initialize the objects Lightweight container can wrap an object with a proxy Proxy masquerades as the original object Proxy executes arbitrary code before and after method call Spring uses proxies for: transaction management Security 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

15 Copyright (c) 2005 Chris Richardson. All rights reserved.
Agenda Overview of enterprise Java Developing with POJOs and lightweight frameworks Effective testing strategies 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

16 POJO application design
POJO facade Domain model Database access 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

17 Copyright (c) 2005 Chris Richardson. All rights reserved.
Testing overview Challenge You need lots of tests (at every level) You want the tests to run quickly Databases are slow Solution Test without the database where possible Use an in-memory database such as HSQLDB Without a database (fast) Test the business logic and repositories with mock objects Test the object/relational mapping With a database (slow) CRUD tests for persistent objects Test the queries With a database (fast) Test the database schema 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

18 Testing the domain model
Goal: Test the business logic Top-down, test-driven development Service => Domain entity,… How: Use fast, easy to write, in-memory tests Use mocks for an object’s collaborators Especially the repositories, which access the DB 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

19 Mock object tests for the repositories
Repository = domain model class that wraps the ORMF Problem: Bugs in the logic of the repositories Testing against the DB is slow Solution Use mock objects for the ORMF APIs 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

20 Test the object/relational 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 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

21 Copyright (c) 2005 Chris Richardson. All rights reserved.
Testing the schema Problem: Object/relational mapping can reference non-existent tables and columns Only some execution paths will access those tables and columns Solution Write a test that verifies that all referenced tables and columns exists How Persistence framework-specific 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

22 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 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

23 Copyright (c) 2005 Chris Richardson. All rights reserved.
Testing 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 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

24 Copyright (c) 2005 Chris Richardson. All rights reserved.
Testing the façade Façade = encapsulates the business logic Mock object tests Mock the domain objects called by the facade Integration tests: Façade is a good place to test the business tier independently of the UI Write tests for the façade that access the database 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

25 Copyright (c) 2005 Chris Richardson. All rights reserved.
Summary Use POJOs and lightweight frameworks instead of EJB Test at every level Test without the database as much as possible 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.

26 Copyright (c) 2005 Chris Richardson. All rights reserved.
For more information Blog: My book (4Q05): POJOs in Action 4/7/2019 Copyright (c) Chris Richardson. All rights reserved.


Download ppt "Developing and testing enterprise Java applications"

Similar presentations


Ads by Google