Search code examples
javagwtjunitgwttestcase

GWTTestCase: JUnit Assertions within callback raises JavaScriptException


So if I call any JUnit assertion which fails from inside a callback method I get this exception:

Mar 12, 2012 11:24:41 AM 

com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl runJob
SEVERE: Job run failed with unexpected RuntimeException: [object Class JavaObject] (injected script#14)
net.sourceforge.htmlunit.corejs.javascript.JavaScriptException: [object Class JavaObject] (injected script#14)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1062)
    at script(injected script:14)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:845)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:429)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:269)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3162)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:559)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.setState(XMLHttpRequest.java:181)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(XMLHttpRequest.java:525)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.access$000(XMLHttpRequest.java:64)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$1.run(XMLHttpRequest.java:461)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$2.run(XMLHttpRequest.java:467)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runJob(JavaScriptJobManagerImpl.java:226)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:307)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor.run(JavaScriptExecutor.java:182)
    at java.lang.Thread.run(Thread.java:722)

A simple test that produces this:

public void testSimple() {
    MyPrgrmServiceAsync serv = GWT.create(MyPrgrmService.class);
    serv.search("some query", new AsyncCallback<SearchResult>() {
        public void onSuccess(SearchResult sr) {
            fail();
        }

        public void onFailure(Throwable caught) {
            fail(caught.toString());
        }
    });

    delayTestFinish(60000);
}

When this test is run, it "passes" as far as the JUnit Runner is concerned with the green bar displayed in Eclipse, but obviously it's supposed to fail. The only indication that anything went wrong is the exception in the console. If I move the fail() outside the callback, it fails normally and indicates so. Additionally, assertions that resolve to true behave normally, so it seems the test simply doesn't know how to detect failures when they arise from within anonymous classes. Is there something I'm not understanding correctly about how to use JUnit assertions within callbacks/anonymous classes with respect to GWTTestCase?


Solution

  • The problem was I was calling finishTest() in my gwtSetUp(). I was running some initialization code there which was an asynchronous method, and wanted to make sure it finished before the actual test code, but turns out it was just finishing the test before anything was actually tested. Moral of the story: Don't be silly about your finishTest() placement.