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:

java.lang.IllegalStateException: 
No WebApplicationContext found: no ContextLoaderListener registered? 
at org.springframework.web.context.support.WebApplicationContextUtils.
getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)

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.

@RunWith(MockitoJUnitRunner.class)
public class TestHomePage
{
	private WicketTester tester;


	private YourWicketApplication app;

	@Mock
	private Service aService;

	@Before
	public void setUp() throws IOException, Exception
	{
		MockitoAnnotations.initMocks(this);
		tester = new WicketTester(app = new YourWicketApplication(){
                        
      /**
			 * adjust the servletcontext to contain a WebApplicationContext
			 */
			@Override
			public ServletContext getServletContext() {
				ServletContext servletContext = super.getServletContext();
				XmlWebApplicationContext  applicationContext = new XmlWebApplicationContext();
				applicationContext.setConfigLocation("classpath:applicationContext.xml");
				applicationContext.setServletContext(servletContext);
				applicationContext.refresh();
				servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);

				return servletContext;
			}

		});

    // replace spring bean with a mock
		app.setService(aService);

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

		when(aService.getList()).thenReturn(list);
	}

	@Test
	public void homepageRendersSuccessfully()
	{
		//start and render the test page
		tester.startPage(HomePage.class);

		//assert rendered page class
		tester.assertRenderedPage(HomePage.class);
		verify(aService).getList();
	}
}

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.