Integration Testing with Spring Framework + JSF
In all projects we supposed to have three kinds of testing: Unit Testing, Integration Testing and Functional Testing.
Unit Testing is about the smaller piece of a software, the method. In this testing, we can use tools such as: JUnit, TestNG, EasyMock, JMock and so on.
Functional testing is a little harder to implement, however tools like Selenium let our life easier. Usually I suggest the web team to get started their job thinking about Selenium testing, in this way, they build the view tier using propers ids, components and so on.
Whether unit testing is responsible for single methods, what about the integration between them? what about the integration between the software and external resources such as databases and files? For this issue, we’ve got the integration testing.
Spring team thinks like mysefl: Integration testing is important and it should be performed without a container. but what does it mean? Many tools for integration testing, like Cactus, requires the container running to perform testing, even a single testing. In my experience, I notified that start up a container just to perform testing is painful.
Spring has its own spring-test module that eliminates the needed to run integration testing with the container online. Thus, you can test your bean, service layer and even DAO layer without the container. This post is gonna show up an example about integration testing with spring framework. You can reach more details about it in the spring official documentation.
Our example - Login application

Almost all applications have a login system. Let’s use this approach as example. In our application, we’ll have the JSF (JSF page + managed bean) in the view tier, application service class in the business tier and the dao class in the integration tier. We’re gonna use Hibernate as persistence layer and its entities as business object.
The managed bean is gonna be called: LoginMBean. It will have an instance of LoginService (our service) and the DAO is gonna be injected into the LoginService. Also, the transaction demarcation is gonna be inside the application service. Finally, all of these components are gonna be mapped with annotations. Thanks Spring 2.5 for provide us annotations.
Note: It’s not the best architecture for all applications, but it looks good for our example.
Downloading the project
I’ve created a project using maven webapp archetype. It is the easiest way to create a project, as well as add the required dependencies. Make sure you have Maven installed and then download the project here.
Unzip the project and then you can open it either with Eclipse (using import -> Existing project into workspace) or Netbeans (open project). Below following some details:
- The JUnit version used is 4.4. If you try the version 4.5 or 4.6, youll get exception. It’s a known bug that spring team is fixing in version 3.0
- LoginService is annotated with the @Service annotation
- We’re using the GenericDAO provided by Hibernate. It’s annotated with @Repository annotation.
- The injection are done using the @Autowired annotation
- In the applicationContext.xml file has only the SessionFactory and the transaction demarcation
- The application uses HSQL database. For testing purpose, we’re using the memory approach
Creating the Integratin Testing class
By default, the testing in maven are located into src/test/java folder. In the project you’ve downloaded, I left the LoginMBeanIntegrationTesting already done. Below following its code and the explanation.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/applicationContext.xml"}) /** * @author Jair Rillo Junior */ public class LoginMBeanIntegrationTest { /* * ManagedBean component. */ private LoginMBean loginMBean; /* * Service object. It's gonna be injected into ManagedBean */ @Autowired private LoginService loginService; @Before public void setUp() throws Exception { //Start LoginMBean loginMBean = new LoginMBean(); //Inject LoginService loginMBean.setLoginService(loginService); //By default, insert a sample user into database User user = createUser("jair","test123"); //Insert the user loginService.insertUser(user); } @After public void tearDown() throws Exception { loginMBean = null; } /** * Test a login with correct information * @throws Exception */ @Test public void testLoginSuccessful() throws Exception { //Create a user which exists into database User user = createUser("jair", "test123"); //Inject the User into ManagedBean. //THis is the same behavior of JSF loginMBean.setUser(user); //Test if the return is "success" Assert.assertEquals("success", loginMBean.doLogin()); } /** * Test a login with incorrect information * @throws Exception */ @Test public void testLoginError() throws Exception { //Create a user with wrong password User user = createUser("jair", "test1234"); //Inject the User into ManagedBean. //THis is the same behavior of JSF loginMBean.setUser(user); //Test if the return is "success" Assert.assertEquals("error", loginMBean.doLogin()); } private User createUser(String username, String password) { User user = new User(); user.setUsername(username); user.setPassword(password); return user; } }
You can run the code and hopefully a green screen will show up.
![]()
On line 1 and line 2 above are located the resources that Spring provide us. In the first line, we tell to Spring to use its class runner. The second one is the location where is our applicationContext.xml. Note: Spring will search for this file into the classpath. If it is in another location, no problem, you can use the file schema, e.g: @ContextConfiguration(locations={”file:/src/main/java/webapp/WEB-INF//applicationContext.xml”})
If you check the code done (ManagedBean, Service and DAO) you will see how the resources are injected. In a normal integration testing, we should inject them ourself, however using the spring class runner, the resources are injected for itself.
Conclusion
Using this approach, you can run integration testing in a faster way. If you need the container, then move out the testing to the functional testing area.
As we can see in this post, making integration testing with Spring is really simple and efficient. If you’re using Spring in your project, do not hesitate to make your integration testing using Spring-Test module.
Thanks spring team for this great job.
If you have any question or comment, leave your message below.

Brazilian guy, IT Specialist, Linux User, IBM Certified SOA Fundamentals, Rational Developer, Sun Certified Java Associate 1.0, Sun Certified Java Programmer 1.4, Sun Certified Web Component Developer 1.4 and Sun Certified Business Component Developer 5. Also Ruby and Python enthusiastic.
Thanks, Junior, you saved my day.