<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Just a Thought...: Wicket, Spring, Hibernate, Testing... Yeah, it's like that.</title>
    <link>http://bloritsch.d-haven.net/articles/2009/11/10/wicket-spring-hibernate-testing-yeah-its-like-that</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Random thoughts</description>
    <item>
      <title>Wicket, Spring, Hibernate, Testing... Yeah, it's like that.</title>
      <description>&lt;p&gt;I have to deliver a product using Java.  Due to politics and approved baselines, etc.  I can&amp;#8217;t use Ruby on Rails.  I&amp;#8217;ve done my own thing in the past, and so this time I decided to save some time by incorporating Wicket, Spring, and Hibernate using auto-wiring, and attributes.  Getting it all to talk together took the better part of a day, but I got it to deploy and run fine.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m an avid unit tester, so I was happy to learn that Wicket has some facilities to make testing a bit easier without forcing you to create your own Servlet mock objects.  I was really happy about that, so I decided to try it out.  Unfortunately, I ran into a rather nasty roadblock.  Spring was complaining about the ContextLoaderListener not being loaded.  It&amp;#8217;s defined in my web.xml file, but the autowiring requires the spring context to be loaded in a particular location.  Finding out how to fix that problem took me the better part of half a day.&lt;/p&gt;


	&lt;p&gt;The problem is that Wicket hides the &lt;code&gt;ServletContext&lt;/code&gt; object away from you.  You can&amp;#8217;t add values, because the test framework is initialized on construction.  Attempts to obtain the ServletContext and manually add the Spring web application context just weren&amp;#8217;t working.  That&amp;#8217;s because it is configured when the &lt;code&gt;WicketTester&lt;/code&gt; object is constructed and ignored after that.  I needed to be able to inject my own ServletContext with the Spring integration taken care of.  I finally figured it out here, because the forums just working for me.  Please note that this is tested with Wicket 1.4 and not any of the earlier versions:&lt;/p&gt;


&lt;pre&gt;
final MyApplication app = new MyApplication();
tester = new WicketTester(app) {
    @Override
    public ServletContext newServletContext(final String path) {
        // web context
        ServletContext context = new MockServletContext(app, path);
        // spring context
        XmlWebApplicationContext spring = new XmlWebApplicationContext();
        spring.setServletContext(context);
        // configure spring
        spring.setConfigLocation("classpath:application.xml");

        // put spring where Wicket can find it
        context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, spring);
        return context;
    }
};
&lt;/pre&gt;

	&lt;p&gt;So let me explain what I did.  I created an anonymous subclass of the &lt;code&gt;WicketTester&lt;/code&gt; class so I could override the factory method &amp;#8220;newServletContext&amp;#8221; to inject my spring configuration.  Done properly, the wicket folks can take care of this by providing a &lt;code&gt;SpringWicketTester&lt;/code&gt; subclass in their spring integration library.  The SpringWicketTester would let you specify the location of your Spring configuration file, and it would override that method for you.  For now, you&amp;#8217;ll have to do it yourself.&lt;/p&gt;


	&lt;p&gt;I put the WicketTester initialization in a subclass of the JUnit &lt;code&gt;TestCase&lt;/code&gt; class.  That way I don&amp;#8217;t have to repeat myself for setting up my Wicket testing.&lt;/p&gt;</description>
      <pubDate>Tue, 10 Nov 2009 16:57:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9a6bec44-047e-4fbb-b82e-81f78ce4d1f0</guid>
      <author>Berin Loritsch</author>
      <link>http://bloritsch.d-haven.net/articles/2009/11/10/wicket-spring-hibernate-testing-yeah-its-like-that</link>
      <category>wicket</category>
      <category>wicket14</category>
      <category>spring</category>
      <category>hibernate</category>
      <category>junit</category>
      <category>testing</category>
      <category>framework</category>
      <category>web</category>
      <category>java</category>
    </item>
  </channel>
</rss>
