public class MyTestCase extends TestCase {
private JsTester jsTester;
protected void setUp() throws Exception {
jsTester jsTester = new JsTester();
// initialize the tester
jsTester.onSetUp();
}
protected void teardown throws Exception {
// cleanup, don't forget!
jsTester.onTearDown();
}
}loadScript()
method and pass the result to eval() before the validator is needed.
Tipically all validators can be initialized after onSetUp() was called.
protected void setUp() throws Exception {
jsTester jsTester = new JsTester();
// initialize the tester
jsTester.onSetUp();
jsTester.eval( jsTester.loadScript("myvalidators.js"));
}eval() as many
times as needed.
public class MyTestCase extends TestCase {
private JsTester jsTester;
protected void setUp() throws Exception {
jsTester jsTester = new JsTester();
// initialize the tester
jsTester.onSetUp();
jsTester.eval( jsTester.loadScript("myvalidators.js"));
}
protected void tearDown throws Exception {
// cleanup, don't forget!
jsTester.onTearDown();
}
public void testMyJsScript(){
jsTester.eval( jsTester.loadScript("myscript.js") );
// myobject is a variable created inside myscript.js
jsTester.assertNotNull("myobject");
}
public void testInlineJs(){
jsTester.eval("var helloFunc = function(){ return 'hello world'; };");
jsTester.assertIsFunction("helloFunc");
}
}When all you want to do is test javaScript code in isolation, your best option is to extend JsTestCase. This class uses a JsTester internally to test your code.
public class MyTestCase extends JsTestCase {
public MyTestCase( String testName ){
super( testName );
}
}loadScript()
method and pass the result to eval() before the validator is needed.
Tipically all validators can be initialized inside the setUp().
protected void setUp() throws Exception {
// don't forget to call super.setUp()
// or the JsTester won't be initialized
super.setUp();
eval( loadScript("myvalidators.js") );
}eval() as many
times as needed.
Assert your code as previously explained. The only change is that
JsTestCase.assertEquals will not work on your javaScript code, you must use
assertExpressionEquals().
public class MyTestCase extends JsTestCase {
public MyTestCase( String testName ){
super( testName );
}
protected void setUp() throws Exception {
// don't forget to call super.setUp()
// or the JsTester won't be initialized
super.setUp();
eval( loadScript("myvalidators.js") );
}
public void testMyJsScript(){
eval( loadScript("myscript.js") );
// myobject is a variable created inside myscript.js
assertNotNull("myobject");
}
public void testInlineJs(){
eval("var helloFunc = function(){ return 'hello world'; };");
assertIsFunction("helloFunc");
}
}