dinsdag 25 maart 2014

Test with Wicket and Spring

Introduction:

I started working with Wicket and as a Java developer I like the concept. I think when there are really front end developers working, it is harder to defend the fact that you want to use wicket. Wicket brings it own html notations. Most frontenders are not keen on that. For working with Wicket there is a very good starting point: the wicket free guide. The interesting part starts when you are about to use DI and Wicket. Setting that up is not to hard The web.xml should look like this:

<web-app>
  <display-name>MyApp</display-name>
  <filter>
  <filter-name>MyAppFilter</filter-name>
  <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
  <init-param>
  <param-name>applicationClassName</param-name>
<param-value>com.my.app.MyApp</param-value>
  </init-param>
  </filter>
  <filter-mapping>
    <filter-name>MyAppFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

For maven just get the latest version of wicket and spring and you be up and running in no time.

The problem:

The thing i ran into was the part where I wanted to start testing with Wicket using some DI to deal with some Classes which otherwise would end up in a very complex design. So the thing I cooked up after reading a lot of examples on the internet that didn't work.

The spring test config in src/test/resources:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  <bean id="classa" class="com.myapp.ClassA">
</bean>
  <bean id="classb" class="com.myapp.ClassB">
</bean>
 <bean id="wicketApplication" class="com.myapp.MyWicketApp">
</bean>
</beans>

The Junit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:myApp-test-context.xml" })
public class LoginTest {
private WicketTester wicketTester;

@Inject
        // this stub is inheriting from an abstract class which is enhiriting from the wicket
          WebApplication otherwise I cannot DI with the same type. The real app is also inheriting from
           that abstract class
private AmyApp myAppStub;

@Before
public void init() {
wicketTester = new WicketTester(myAppStub);
}
}

The mock Application:

public class MyAppStub extends AmyApp {
private final ApplicationContextMock applicationContextMock = new ApplicationContextMock();
       //injecting the classes from the test context.
@Inject
private ClassA classa;
@Inject
private ClassB classb;

protected ApplicationContextMock getApplicationContextMock() {
return applicationContextMock;
}

@Override
public Class<? extends Page> getHomePage() {
// TODO Auto-generated method stub
return null;
}

@Override
public void init() {
                //Adding the injected classes to the context mock so they will be used during testing.
applicationContextMock.putBean("classa",
classa);
applicationContextMock.putBean("classb",
classb);
                //adding the applicationmock to spring
getComponentInstantiationListeners().add(
new SpringComponentInjector(this, applicationContextMock));
}

}

Conclusion:

After I figured out how this baby works it is not that hard anymore to test with wicket. All you have to do is figure out the right pieces of the puzzle. In all the other examples the same pieces where missing.

I hope This helps.

Have Fun!

Geen opmerkingen:

Een reactie posten