Friday, December 27, 2013

spring integration tutorial

I was checking if we could migrate some of our project's code to Spring Integration and I found a very good tutorial with a not-so-basic example.
Check this two part tutorial:
Spring Integration part 1
Spring Integration part 2

Wednesday, November 20, 2013

sonarqube presentation

I did a SonarQube presentation at work. Feel free to check and use it as you like here.

Wednesday, July 17, 2013

java.lang.illegalstateexception: missing behavior definition for the preceding method call

I have the following exception while mocking a final method with PowerMock.

java.lang.illegalStateException: missing behavior definition for the preceding method call

My previous code was:

 ReloadableResourceBundleMessageSource mockMessage =
null, Locale.ENGLISH)).andReturn("error_message");

Then I realized that createMock() and replay() belongs to EasyMock not PowerMock.
I explicitly did these calls:

 ReloadableResourceBundleMessageSource mockMessage =
PowerMock.createMock(ReloadableResourceBundleMessageSource .class);
null, Locale.ENGLISH)).andReturn("error_message");

But that's not enough. For PowerMock to work, you'll also need to add few annotations before the class definition.


Notice that @PrepareForTest can also be defined just before the test method.
After these fixes my test begins to work.

Sunday, July 14, 2013

non-parseable settings in maven

I saw a very odd error while building our project with maven.

[ERROR] Error executing Maven.
[ERROR] 1 problem was encountered while building the effective settings
[FATAL] Non-parseable settings C:\Program Files
(x86)\Apache\apache-maven-3.0.5\conf\settings.xml: entity reference
names can not start with character '<' (position: START_TAG seen
...myPassword<... @12:33)  @ C:\Program Files
(x86)\Apache\apache-maven-3.0.5\conf\settings.xml, line 12, column 33
Nothing in the project changed so it shouldn't spit me an error. Then I saw the error line and remembered that I changed my corporate password and put it in the settings.xml because our corporate proxy need it for working. So if you see a similar error, keep in mind that this is because and invalid character in the xml file. My password contained an ampersand and thus I had this issue.

Monday, May 13, 2013

inject a value in a static field using spring

I don't know any software developer who's a great fan of legacy code but as any developer I know that there's no escape from it too. There's a utility class that is used everywhere in our project. I'd like to inject two arguments in it and then use them in another method. But every method of this class is static and Spring does not like injecting stuff in static classes. One way to do it is to add a bean definition for this static class (as if this is not a utility class but a singleton class) and inject the variables. But a better way is to use static method call using MethodInvokingFactoryBean of Spring.

Our bean definition in the application context will be like this: 


We don't touch the class part. It should be "MethodInvokingFactoryBean". We will give two properties. The "staticMethod" property will tell Spring the class and its method where we will inject the arguments. The second property (arguments) will tell Spring which arguments to be injected. I'm injecting a list of arguments here. The list consists of "apology" and "accepted". I have my bean definition but where's my class? Here it is:
public class ExampleUtil {
 private static String argument1, argument2;
 public static void setArguments(String arg1, String arg2){
  argument1 = arg1;
  argument2 = arg2;

 public static void printArguments(){
  System.out.println("args: "+argument1+", "+argument2);

If you debug you'll try that two Strings (apology and accepted) is injected to ExampleUtil's setArguments() method. As they are static fields they can be reached from other methods such as printArguments().

Friday, April 26, 2013

deleting browser cookies (in java)

In our implementation, there's a need for a session cookie that will be created during the user login and that should be deleted during the logout. Everything I read on Google tell the same thing:
  1. Get the cookie object from the request
  2. Set its max age to 0
  3. Add the cookie to the response
This should be quite simple right? But I still see my cookie even if I implement these steps.
After a little bit of search and a little try, I see that I must set cookie path as "/" as well. So the result code is as below. getCookie() takes the cookie from the request object, and deleteCookie() deletes it.

 public Cookie getCookie(HttpServletRequest request, String cookieName) {
  if (request != null && cookieName != null) {
   Cookie[] cookies = request.getCookies();
   if (cookies != null) {
    for (Cookie cookie : cookies) {
     if (cookieName.equals(cookie.getName())) {
      return cookie;
  return null;

 public void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
  Cookie cookie = getCookie(request, cookieName);
  if (cookie != null) {