enterprise spring examples and integration tests

Enterprise Spring examples and integration tests

There was longer gap in my blog, because I was busy writing book Pivotal Certified Spring Enterprise Integration Specialist Exam – A Study Guide. This book is example heavy. Fortunately Apress agreed to open source all these examples. Therefore there is quite extensive collection of Spring examples available in this GitHub repository. Altogether  146 Spring examples with integration tests.

Most of the examples use Spring Boot. But as book is not focused on this modern framework, therefore there are included also plain Spring Framework examples. So is is possible can compare how Spring Boot can simplify Spring developer’s life. Most of the examples are covered with Java and also with XML configuration (where it make sense), therefore it provides also good comparison between these two approaches.

Examples cover main Enterprise Spring features of:

  • Spring Tasks and Scheduling
  • Spring Remoting (RMI, HttpInvoker, Hessian, Burlap)
  • Spring Web Services (SOAP)
  • REST Services with Spring (Spring MVC and JAX-RS)
  • Spring JMS
  • Spring JMS transactions and acknowledge modes
  • Distributed Transactions with Spring
  • Spring Integration
  • Spring Batch

As I am test infected, I wanted to make sure that examples are working. Therefore extensive test suite was created, where each example is backed up by integration test. These tests can be useful as ideas for integration testing of Spring applications.

Few examples are used to highlight error handling features of Spring, therefore there are sometimes errors simulated in example logic. Book would be needed to fully understand intentions of these examples. Some examples cover advanced Spring topics (e.g. Distributed Transactions, Spring Batch Remote Chunking or Spring Batch Remote Partitioning). Examples for these topics are not very common on web, so these simplistic examples can help grasp advanced these Spring features.

Hope somebody will find this battery of examples and tests valuable and possibly give a chance to this book.

mock Spring bean, encapsulate spring bean

How to encapsulate Spring bean

As far as I know Spring Framework doesn’t provide any mechanism to encapsulate Spring beans other than having separate contexts. So when you have public class registered in Spring’s Inversion of Control container, it can be autowired in any Spring bean from same context configuration. This is very powerful but it is also very dangerous. Developers can easily couple beans together. With lack of discipline team can easily shoot themselves in foot. Unfortunately I was working on one monolithic project where team was shooting themselves into foot with submachine gun. Wiring was breaking layering rules often. Nobody could easily follow what is dependent on what. Bean dependency graph was just crazy. This is serious concern in bigger applications.

Luckily there is one simple way how to encapsulate Spring bean. Spring works nicely with default access modifier on class level. So you can create package private bean, which can be used only within current package. Simple and powerful. Let’s take a look at example:

package net.lkrnac.blog.spring.encapsulatebean.service;

import org.springframework.stereotype.Service;

class AddressService {
	public String getAddress(String userName){
		return "3 Dark Corner";

This simple bean is wired into another one within same package:

package net.lkrnac.blog.spring.encapsulatebean.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class UserService {
	private AddressService addressService;

	public UserService(AddressService addressService) {
		this.addressService = addressService;
	public String getUserDetails(String userName){
		String address = addressService.getAddress(userName);
		return String.format("User: %s, %s", userName, address);

Main context just scans both beans:

package net.lkrnac.blog.spring.encapsulatebean;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class Application {

Here is test to prove it works fine:

package net.lkrnac.blog.spring.encapsulatebean;

import net.lkrnac.blog.spring.encapsulatebean.service.UserService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
	private UserService userService;
	public void isPackagePrivateBeanCalled(){
		//GIVEN - spring context defined by Application class
		String actualUserDetails = userService.getUserDetails("john");
		Assert.assertEquals("User: john, 3 Dark Corner", actualUserDetails);

I believe everybody should consider using default access modifier for every new bean. Obviously there would need to be some public bean within each package. But at not every bean. Source code is on GitHub.