Unit Testing a Spring Controller

I have created a Spring 3.1 controller using the @MVC annotation and I was not too sure how to test it. When googling around I was introduced to the many mock helper classes that exist but I was not sure how to make it work. So I started with something simpler. Here is my unit test code for the HomeController that is created in the template from STS.

package com.cinq.test;

import java.util.Locale;

import org.junit.Before;
import org.junit.Test;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;

 * @author mc
public class HomeControllerTest {

	private HomeController homeController;
	private Model model;
	private Locale locale;

	 * @throws java.lang.Exception
	public void setUp() throws Exception {
		homeController = new HomeController();
		model = new ExtendedModelMap();
		locale = new Locale("fr", "CA");

	public void TestHomeController() {
		assert(homeController.home(locale, model).compareTo("home") == 0);


As you can see there is not much to it. I could easily add a validation that the serverTime is not too far from the current time.

Next I have to figure out how to test a controller that is meant to answer Ajax calls.

IE and caching AJAX calls in my Spring webapp

When things work fine in Chrome and Firefox but not in IE I am quick at hating MS. Really quick!

In one webapp I have a small widget that allows to decide which columns to show or hide. It does AJAX calls to get the users preferences and to save them when they are modified.

The problem in IE is that it would not get the new preferences as we moved in the webapp. It would keep the original preferences.

If you restart the browser then it would picked the new display preferences. We then configured IE to get new content on every page request and that also “fixed” the issue.

I decided to look to see what configuration I needed to add to my Spring 3.1 app to specify the caching on the AJAX request. I found this answer in Stackoverflow.

From that I basically added this xml to my servlet.xml file:

<!– caching settings for some URIs –>
<bean class=”org.springframework.web.servlet.mvc.WebContentInterceptor” p:cacheSeconds=”0″ p:alwaysUseFullPath=”true” >
<property name=”cacheMappings”>
<!– don’t cache –>
<prop key=”/path/to/ajax/**”>0</prop>

I also needed to add xmlns:p=”http://www.springframework.org/schema/p&#8221; to my beans statement since I was not using the p namespace for anything else.

Rich Web Application with Spring Framework

I just completed 4 days in a class room on this subject.

A little bit overwhelming.

A lot of good material but it is way too much to digest in just 4 days.

I wish training companies would offer scheduling alternatives to these straight X days hellish setup. I think that offering 2 half days a week for a month would be easier to digest.

After taking this course I have a voucher to go try the exam. I will take it and to prepare I received a few advices that I like:

  1. Set a date for the exam so you have the deadline motivation working for you
  2. Use the study guide questions from Spring Source
  3. Write all the answers on a cheat sheet and carry it with you every where you go to read it over and over
  4. Redo all the labs and test alternative solutions

So by Monday I will have my schedule written down and I will start on my certification path. I know that it will be a lot of work but I see all the benefits from it and that motivates me more than the deadline.