SyntaxHighlighter

Showing posts with label Testing. Show all posts
Showing posts with label Testing. Show all posts

Thursday, March 28, 2013

Mockito Pointers

Mockito Pointers

here's a couple of pointers on Mockito that i don't want to lose;

mock something


   ServletContext servletContext = mock(ServletContext.class);


control the response


    when(servletContext.getContextPath()).thenReturn("some value");


when you don't know the argument value


  when(servletContext.getRealPath(anyObject()).thenReturn("something");

Wednesday, July 11, 2012

Spring Integration - Testing Strategies (Part 1)

Separation of Adapters from Channels

one of the interesting little testing techniques i've encountered with Spring Integration is to create separate xml files for your channel logic and your adapters.  this helps in two ways;
- clearly separates your 'logic' from your communications channels (e.g. JMS from Routers)
- can assist with unit testing by attacking separation of concerns

so here's the 'logic' part of spring integration



and here's the 'communications channels' (a simple http inbound gateway)



this will allow us to JUnit test just the logic without testing the http gateway




Wednesday, July 4, 2012

Spring Integration - Managing a Service at Runtime


one of the neat tricks to having Spring Integration running is being able to dynamically stop/start it based on other environmental factors.  that is, we've got a long running, Spring Integration polling process going, checking for and processing files every few seconds.  now, at a certain interval during the day, we start a batch process which needs all the resources it can get.  so, whether or not this is a regularly scheduled event, we want to pause the file processor until such time as the batch job is complete.

how?

we can use a Control Bus


in this situation, the solution can be to setup a separate process that monitors or listens for the batch start event.  when a 'positive' event is received, a message is then sent to the Spring Integration Control Bus to 'start'/'stop' certain components.  in the example included below, the business case is as follows;

1. poll a directory for files every x interval
2. poll a database for a result every y interval
3. if the database poll comes back positive, stop the directory/file polling
4. if the database poll comes back negative, resume the directory/file polling

here's the Spring Integrations for the file process

file monitor



monitor the batch process



datasource config (for testing purposes)



task executor configs



hopefully this will help with the problem of dynamically monitoring

Wednesday, June 27, 2012

Spring Integration Testing Hints - Logging


ever wanted to trace a message through Spring Integration?  and of course, you've added your own interceptors and coded a lot of junk to do it?  code no more...

simple hint - turn on debugging (e.g. log4j DEBUG)

you'll see the message pass through the channels, services, etc. and their contents as they do.  very handy way to debug