We added JUnit integration to our Java to Javascript code generator ST-JS (http://st-js.org). The framework generates to corresponding Javascript for both the tested code and the unit tests and sends the code to different browsers.
There is no need for a separate server as the unit test runner opens the needed http port (and closes it once the tests finished). The framework manipulates the Java stacktrace so that the failed asserts are correctly displayed by the JUnit Eclipse plugin. Here is a simple example with jQuery and Mockjax:
@RunWith(STJSTestDriverRunner.class)
@HTMLFixture("<div id='fortune'></div>")
@Scripts({ "classpath://jquery.js",
"classpath://jquery.mockjax.js", "classpath://json2.js" })
public class MockjaxExampleTest {
@Test
public void myTest() {
$.ajaxSetup($map("async", false));
$.mockjax(new MockjaxOptions() {
{
url = "/restful/fortune";
responseText = new Fortune() {
{
status = "success";
fortune = "Are you a turtle?";
}
};
}
});
$.getJSON("/restful/fortune", null, new Callback3<Fortune, String, JQueryXHR>() {
@Override
public void $invoke(Fortune response, String p2, JQueryXHR p3) {
if (response.status.equals("success")) {
$("#fortune").html("Your fortune is: " + response.fortune);
} else {
$("#fortune").html("Things do not look good, no fortune was told");
}
}
});
assertEquals("Your fortune is: Are you a turtle?", $("#fortune").html());
}
private static class Fortune {
public String status;
public String fortune;
}
}