1. Start Spring application context for your Tapestry unit tests

    Tapestry has a powerful tool to unit test your pages and services, but how to deal with it when your pages and services use Spring beans ?

    Tapestry PageTester can be extended like the TapestryFilter to provide your own ServiceModuleDef classes.  The tapestry-spring contribution already provides the one needed to load a Spring application context. Then, loading Spring for your unit tests will be as simple as extended the existing PageTester and provide the Tapestry Spring Module Definition class.

    Also, because this module definition uses the servlet application context to get the configuration files location, we will have to mock it. Let’s use Spring MockServletContext for this.

    • Update your maven dependencies to add spring web mock objects
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring-version}</version>
      </dependency>
    
    • Extend Tapestry PageTester, adapt the code below to your needs if you want to provide mock versions of your spring beans or make it more configurable
    public class WookiPageTester extends PageTester
    {
    
     private MockServletContext servletContext;
    
     public WookiPageTester(String appPackage, String appName, String contextPath,
     Class... moduleClasses)
     {
       super(appPackage, appName, contextPath, moduleClasses);
       Registry registry = this.getRegistry();
       // Set Tapestry registry in mock servlet context
       servletContext.setAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME, registry);
     }
    
     public WookiPageTester(String appPackage, String appName)
     {
       super(appPackage, appName);
     }
    
     @Override
     protected ModuleDef[] provideExtraModuleDefs()
     {
       // Set spring configuration files location
       servletContext = new MockServletContext();
       servletContext.addInitParameter("contextConfigLocation", "classpath*:mock-applicationContext.xml");
       return new ModuleDef[] { new SpringModuleDef(servletContext) };
     }
    
    }
    

    Thanks for reading !

1 Comment »

RSS for comments - TrackBack URL

  1. help,when test

    Unable to delegate method invocation to property ‘HTTPServletRequest’ of , because the property is null.

    how can i fix this error?
    some one can send email to hxzon@163.com and help me?

    ——————————–
    public class MaterialTester extends PageTester {
    private MockServletContext servletContext;

    public MaterialTester() {
    super(“com.ptswitch.material”, “app”, “WebRoot”);
    Registry registry = this.getRegistry();
    // Set Tapestry registry in mock servlet context
    servletContext.setAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME, registry);
    }

    @Override
    protected ModuleDef[] provideExtraModuleDefs() {
    // Set spring configuration files location
    servletContext = new MockServletContext();
    servletContext.addInitParameter(“contextConfigLocation”, “WebRoot/WEB-INF/applicationContext.xml”);
    servletContext.addInitParameter(SpringConstants.USE_EXTERNAL_SPRING_CONTEXT, “true”);
    servletContext.setAttribute(“org.springframework.web.context.WebApplicationContext.ROOT”, new org.springframework.context.support.FileSystemXmlApplicationContext(
    “WebRoot/WEB-INF/applicationContext.xml”));
    return new ModuleDef[] { new SpringModuleDef(servletContext) };
    }
    }
    ————————————
    public static void main(String args[]) {
    PageTester tester = new MaterialTester();
    Document doc = tester.renderPage(“user/login”);
    System.out.println(doc);
    }

Leave a comment

Spam Protection by WP-SpamFree