Wicket and Spring JUnit Test

Just had a hard time to create a Junit Test with Wicket, Spring and Mockito. Finally I succeeded and want to share the solution.

The usecase is simple: a test if rendering of the homepage succeeds. The tricky part was the instantiation of the real Wicket Webapplication ( not a MockWebapplication ) with the Spring Context. Without any special arrangements instantiation of the Webapplication led to the following stacktrace:

No WebApplicationContext found: no ContextLoaderListener registered? 
at org.springframework.web.context.support.WebApplicationContextUtils.

To bypass this I override the method getServletContext in the WicketApplication and give Spring what it needs. This way i can test Wicket pages with Spring and using mocks with Mockito.

public class TestHomePage
	private WicketTester tester;

	private YourWicketApplication app;

	private Service aService;

	public void setUp() throws IOException, Exception
		tester = new WicketTester(app = new YourWicketApplication(){
			 * adjust the servletcontext to contain a WebApplicationContext
			public ServletContext getServletContext() {
				ServletContext servletContext = super.getServletContext();
				XmlWebApplicationContext  applicationContext = new XmlWebApplicationContext();
				servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);

				return servletContext;


    // replace spring bean with a mock

		Serializer serializer = new Persister();
		ListOfStuff list = serializer.read(ListOfStuff.class, new ClassPathResource("listofstuff.xml").


	public void homepageRendersSuccessfully()
		//start and render the test page

		//assert rendered page class

Character encoding with Spring MVC FileUpload

Convinced we tackled the character encoding issues in our webapplication by specifying everywhere UTF-8 and using Spring MVC nifty interceptor solution to set the contentype of the Response to UTF-8, I still found a character encoding issue in our webapplication. The problem appeared to be wrong paths of files, but as I dug deeper the cause lay in the solution to upload files where the name of the uploaded file would be used for the path. The name of the file got scrambled in the case special characters were used.

I used the CommonsPortletMultipartResolver class of the Spring MVC framework to handle the uploaded files. Wandering through Spring’s API I found out that their "Sensible Defaulting" paradigm led to adopt the Servlet spec default for the header encoding, which is ISO-8859-1.

Is it wrong to consider UTF-8 as the sensible default ?  I know Sun has made some glitches with the Servlet spec, but I expected the Spring guys to be more pragmatic about encoding. I mean, aren’t we all trying to move to UTF-8 to overcome the character issues ? In the end the first responsibility is ofcourse with the developer using the Spring Framework, yep that’s me, to specifiy the desired encoding. So playing the blame game leads to my own humble me.