About this Tutorial
This tutorial creates a Business Facade class (and a JUnit Test) to talk to the DAO you created in the Persistence Tutorial.
In the context of AppFuse, this is called a Manager class. Its main responsibility is to act as a bridge between the persistence (DAO) layer and the web layer. It's also useful for de-coupling your presentation layer from your database layer. Managers should also be where you put any business logic for your application.
You will learn two things:
- You don't need to write Managers if you just need generic CRUD functionality.
- How to write Managers when you need custom functionality.
Table of Contents
- Register a personManager bean definition
- Create a Manager Interface
- Create a Manager Test to test finder functionality
- Create a Manager Implementation
- Run the Manager Test
- Register your Manager Implementation
- Write the Manager Test using EasyMock
Register a personManager bean definition
AppFuse doesn't require you to write a Manager to persist a POJO. You can use the GenericManager class if all you need is CRUD on an object. To register a personManager bean, its best to wrap the personDao bean.
Open your src/main/webapp/WEB-INF/applicationContext.xml and replace the personDao bean with the following.
If you wrote the
PersonDao interface and implementation in the previous tutorial, you'll want to use the following for your personManager bean definition. If you don't, your
PersonDaoTest will fail because there's no longer an exposed personDao bean.
Once you've created a personManager bean definition, you can use this bean on an object by adding the following setter method:
That's it! To persist an object with AppFuse, all you need to do is:
- Create a POJO with annotations.
- Register it with your persistence framework.
- Write some XML to register a type-safe class to persist it.
Not only did you write data access code, you also used interfaces to abstract your implementation. This means that it should be possible to replace your persistence framework at any time.
Create a Manager Interface
First off, create a
PersonManager interface (in the src/main/java/**/service directory - you may need to create this first) and specify the finder method for any implementation classes.
Create a Manager Test to test finder functionality
Now you know what you want your manager to do, so it's time to write the tests. In this example, you will use mock objects to isolate your tests from external dependencies. This means you do not need a database set up to run these tests, and it will be easier to test different scenarios. In a large project, you could even test your Manager class before someone else finished writing the DAO implementation.
You can write your own mock objects, but here you are going to use Mockito. Create
PersonManagerImplTest in src/test/java/**/service/impl:
This will not compile, as you have not created the
PersonManagerImpl class it tests.
Create a Manager Implementation
The next step is to create a
PersonManagerImpl class that implements the methods in PersonManager.
Create PersonManagerImpl.java in src/main/java/**/service/impl.
Now before you run your tests, review your test class to make sure that it will test all possible conditions.
Run the Manager Test
Save all your edited files and try running mvn test -Dtest=PersonManagerImplTest.
Register your Manager Implementation
There are two ways to register your personManager bean at this point: 1) using a @Service annotation or 2) with XML. To use annotations, simply add @Service("personManager") as a class level parameter:
If you'd prefer to use XML, open your src/main/webapp/WEB-INF/applicationContext.xml file and replace the personManager bean with the following:
Using EasyMock instead of Mockito in Unit Tests
Sooner or later, you will want to add extra dependencies into your project.
AppFuse ships with Mockito, but not EasyMock, so this section will show you how to add EasyMock as a project dependency.
Edit your pom.xml file in your project's top level directory. Add EasyMock as a dependency in the <dependencies> element:
EasyMock is only required during testing, and you don't want it packaged in your application, so it's restricted it to a scope of test.
PersonManagerImplTest class you wrote above so it looks as follows:
Now check everything works by running mvn test -Dtest=PersonManagerImplTest again.