Adding Spring Integration to our project (apart from many advantages) brings drawbacks as well. One of them is more difficult testing.
Let's try to test the flow from MessageRouter to Persister via JSONHandler (please refer to the previous post) .
First of all, it won't be a unit test but an integration test as we need to start-up the spring context and test the flow between components.
Let's prepare spring xml config:
We're importing the application config and overriding beans with mockito mocks.
The test would look like:
Spring injects all the required components. We're mocking the behaviour, sending message to the channel and then verifying. Mocks need to be reset as well if we want to have more test methods.
By looking at the source code we don't see which object is mocked. To solve this problem (and simplify the code as well) we can use springockito.
Then in our config file we only need to import the application config without overriding any beans.
The overriding part is done in the test by using annotations:
To use springockito we need to change the context loader to SpringockitoContextLoader.
The @ReplaceWithMock annotation is self-descriptive.
The context is dirtied after execution of each test method which is basically equivalent to resetting the mocks.
Whole project can be found at github.
Showing posts with label spring integration. Show all posts
Showing posts with label spring integration. Show all posts
Thursday, 25 April 2013
Wednesday, 20 March 2013
Spring Integration vs plain Java code
Adding frameworks to our system may solve some problems and cause too much complexity at the same time.
Let's imagine a simple flow:
We have a MessageRouter object that routes received message to either XMLHandler or JSONHandler. The processed message in both handlers are then passed to Persister that is storing the message.
Modelling it is not that difficult. Here's our MessageRouter class:
Handlers:
And finally persister: We can see it in action by running the following class:
Everything seems fine here, but we have a problem - tight coupling.
Message router knows about handlers and handlers know about persister.
We would gain loose coupling and high cohesion if they weren't aware of each other which would automatically make them concentrate on one task only.
Spring Integration can help us achieve this.
It is a separate module of Spring, enabling lightweight messaging within application and supporting Enterprise Integration Patterns.
All the arrows from the above picture will be represented as channels.
The XML Spring context configuration for the channels looks pretty straightforward:
Actually it's not even required as Spring can create them by default.
Apart from the channels, our MessageRouter only role is to return the channel name to which the message will be passed.
Also the handlers and persister need to become Service Activators (their methods will be invoked when the message will go through the channel).
The config for those:
To see it in action we can run the following class:
Notice that the code is concise and simple.
The components do not depend on each other.
What is more, if we wanted to modify the flow and move Persister component in front of the MessageRouter, we would need to change the xml config to:
Changing the flow in the first version that is using plain Java code would require much more modifications.
Nevertheless we increased the complexity of our application. Now we depend on a framework and we need to maintain additional configuration in an XML file.
Another big disadvantage is testing. We could easily unit test the previous code using mocks. Now we need to test the Java code and Spring Integration configuration as well, which is not that simple.
I'll show how to do it in the next post.
Let's imagine a simple flow:
Modelling it is not that difficult. Here's our MessageRouter class:
Handlers:
And finally persister: We can see it in action by running the following class:
Everything seems fine here, but we have a problem - tight coupling.
Message router knows about handlers and handlers know about persister.
We would gain loose coupling and high cohesion if they weren't aware of each other which would automatically make them concentrate on one task only.
Spring Integration can help us achieve this.
It is a separate module of Spring, enabling lightweight messaging within application and supporting Enterprise Integration Patterns.
All the arrows from the above picture will be represented as channels.
The XML Spring context configuration for the channels looks pretty straightforward:
Actually it's not even required as Spring can create them by default.
Apart from the channels, our MessageRouter only role is to return the channel name to which the message will be passed.
Also the handlers and persister need to become Service Activators (their methods will be invoked when the message will go through the channel).
The config for those:
To see it in action we can run the following class:
Notice that the code is concise and simple.
The components do not depend on each other.
What is more, if we wanted to modify the flow and move Persister component in front of the MessageRouter, we would need to change the xml config to:
Changing the flow in the first version that is using plain Java code would require much more modifications.
Nevertheless we increased the complexity of our application. Now we depend on a framework and we need to maintain additional configuration in an XML file.
Another big disadvantage is testing. We could easily unit test the previous code using mocks. Now we need to test the Java code and Spring Integration configuration as well, which is not that simple.
I'll show how to do it in the next post.
Subscribe to:
Posts (Atom)

