How do I get server side initialization code to run when building GWT Test Cases?

842 views Asked by At

I'm using GWT 2.4. I have a number of test files that extend GwtTestCase. My question is, how and where do I place code so that the server side initializes some data before my tests run?

I tried creating a simple servlet ...

public class PopulateCacheServlet extends HttpServlet  {

public void init(ServletConfig config) throws ServletException {
    try { 
        PopulateCache.addTestEntriesToCache();
        System.out.println("Dummy entries written to cache.");
    } catch (Exception e) { 
        e.printStackTrace();
    }
}   // init

}

and adding its definition to my module's .gwt.xml file ...

<servlet path="/populatecache" class="com.myco.clearing.web.PopulateCacheServlet" />

but I'm noticing that this init method is never called.

1

There are 1 answers

0
Adrian B. On

You could simply add a static code block to your PopulateCacheServlet class or initialize the class in the constructor. Static code block if you want to run the method just once and only once (per JVM). Initialization in the constructor if you want to set up each servlet separately. Okay that is not the most beautiful solution, but since it is a test class...

The other option is you call the initialize method from the GwtTestCase. You can use a method annotated with @BeforeClass to call the servlet's init method once before the test is running. Of course you have to make your servlet a GWT RemoteServlet in order for this to work. Otherwise the servlet is not easily accessible from GWT code.