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 (i.e. for Swing apps and web services). 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
The code for this tutorial is located in the "tutorial-service" module of the appfuse-demos project on GitHub. Use the following command to check it out from Subversion:
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.
For JPA, you'll need to change the nested DAO that gets injected into
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:
|The Spring Framework|
To learn more about how Spring works, please see the Spring Reference Guide.
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.
You can also perform all 3 steps with the AppFuse Maven Plugin.
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.
Mockito is quickly becoming the most popular mocking framework for Java and we hope to support it in a future release.
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 registered the personDao using the @Repository("personDao") annotation instead of src/main/webapp/WEB-INF/applicationContext.xml file you will need to auto-wire the PersonDao variable in the PersonManagerImpl class:
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:
The Web application tutorials assume that you will be using the GenericManager. If you follow them after making this change, you will need to change all the references in their code from the GenericManager to your new PersonManager interface. For example:
Using EasyMock instead of jMock in Unit Tests
Sooner or later, you will want to add extra dependencies into your project.
AppFuse currently ships with JMock, 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.
After removing jMock and adding EasyMock to your pom.xml, you'll want to refresh your project in your IDE.
PersonManagerImplTest class you wrote above so it looks as follows:
Note that this class extends junit.framework.TestCase and not an EasyMock class. This makes EasyMock a good choice for annotation based test frameworks such as JUnit 4 and TestNG. Unfortunately (at the time of writing) Maven does not work properly with these frameworks.
Now check everything works by running mvn test -Dtest=PersonManagerImplTest again.