Writing integration tests for RabbitMQ-based components

I’m writing this post after days of misery, reverse-engineering, github browsing and finding (sort of) clever ways to get yet one step closer to the goal mentioned in the title.

Before we dive into the topic, let me clarify a few things. RabbitMQ, an AMQP (advanced message queueing protocol)  compatible message oriented middleware, (in my understanding) has no in-memory message broker implementation. ActiveMQ, another AMQP compatible implementation, on the other hand does provide such a component – easy to configure and use. The problem is that ActiveMQ implements version 1.0 of the protocol, while RabbitMQ is on version 0.9.1, and the two versions, of course, are not compatible. That is the main reason one might need QPID, a third MOM implementation that comes with an in-memory message broker and is able to “speak” multiple versions of the protocol.

Continue reading “Writing integration tests for RabbitMQ-based components”

POSTing multipart requests with RestTemplate

Some days ago I had to write a little client application to call a RESTful web service; my job was to upload some data, defined in terms of JSON, along with a picture (PNG format, but that does not matter for this time). These tasks are usually carried out with POSTing form/multipart messages to the endpoint – my case was no different, the server was expecting just that. I thought it would be easy and fast to throw in RestTemplate from Spring, but as it turned out, this simple task took me around 12 hours, a complete Sunday and a lot of pain (finally, Sotirios Delimanolis pointed me in the right direction on StackOverflow.com). As it seems, there is little amount of code examples out there, so I thought I’d post one here.

Continue reading “POSTing multipart requests with RestTemplate”

Spring: injecting an enum conditionally

The other day I was working on a piece of legacy code, with old-fashioned xml based Spring injection. All of a sudden I found myself in a situation where I had to inject an enum, based on some condition specified in a .properties file. This is easy to do with Java config, but not so usual to me with XMLs, so I thought I’d put it here. Continue reading “Spring: injecting an enum conditionally”

Spring dependency injection – @Configurable

Some time ago I was asked the question whether it is possible to inject dependencies into objects that are not created by Spring. Hmm, sweet. Let’s see if we can do it 🙂

In the EJB world, there is no way to inject dependencies into a bean created with the new operator – as I have already pointed out in an earlier post. In a default Spring setup, an @Autowired class instantiated with the new keyword will behave just like in an enterprise application: all the dependencies will be left untouched; they will not be injected but left with their default values (a.k.a. null). This, usually will end in a NullPointerException like in the example below:

public class CarSalon {
    //...
	public void testDrive() {
		Car car = new Car();
		car.startCar();
	}
}

@Component
public class Car {
	@Autowired
	private Engine engine;
	@Autowired
	private Transmission transmission;

	public void startCar() {
		transmission.setGear(1);
		engine.engineOn();
		System.out.println("Car started");
	}
}

@Component
public class Engine {
//...
}

@Component
public class Transmission {
//...
}

Yep, running this code will fail with a NullPointerException. However, unlike in JavaEE, in Spring there is an awesome way to inject dependencies into objects created with the new keyword: the @Configurable annotation.

In order to make the example above run, you should add the @Configurable annotation to the Car class like this:

@Configurable(preConstruction = true)
@Component
public class Car {

	@Autowired
	private Engine engine;
	@Autowired
	private Transmission transmission;

	public void startCar() {
		transmission.setGear(1);
		engine.engineOn();

		System.out.println("Car started");
	}
}


@Configurable(preConstruction = true) what an annotation! It tells Spring to inject dependencies into the object before the constructor is run. But wait a bit, this will not work just like that, out of the box. You still need to do several more things:

  1. Add the following jars to your classpath (Spring will use aspect orientation in order to work around default behavior, and make your object eligible for dependency injection): aspectj-x.x.x.jaraspectjrt.jar, aspectjveawer-x.x.x.jar
  2. Add a Spring-based java agent to your run configuration like this: -javaagent:”PATH\spring-instrument-x.x.x.jar”

Aaaand pretty much that’s it. The code should now be running and printing “Car started”.

Converting business objects into value objects

In my current project there are two types of data holder objects defined: business objects, used by the server side of the application and value objects (aka data transfer objects) which are very very very (etc.) similar to business objects in terms of properties. These types of value objects are used to pass data to the client. Nothing new so far, this actually happens in pretty many projects.

Continue reading “Converting business objects into value objects”

“Offline” EJB containers

From time to time you may encounter different situations when you want your enterprise beans (both @EJB and @Inject – aka CDI) to be injected independently from the EJB container. Usually in these situations you have no access to any EJB container at all. Such situations can occur when running integration tests, applications where a piece of functionality has to be available on both (non-enterprise)client and server side and many others (there must be other cases… I can’t really think of any right now, but I am certain they exist). Well in these cases you’ve got to inject your stuff somehow, unless you want to end up with a bunch of NullPointerExceptions. Continue reading ““Offline” EJB containers”