JsTester supports the concept of composition, that is why the main class does not extend JUnit's TestCase. In doing so, it can be used with any version of JUnit, including JUnit4 and annotations, and TestNG since version 1.2. It can also be used inside another TestCase hierarchy.
To use the JsTester features you must follow the following steps:
You can use JsTester with any class that extends TestCase, usually creating a private variable as the following example:
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(); } }
You can insert your own validations any time, just call the 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")); }
Evaling the code will run your javaScript code in the rhino engine, affecting the global
scope as intended (that its what we want to assert). Call eval()
as many
times as needed.
JsTester includes the common assert* methods from JUnit (assertNull, assertNotNull, assertEquals, assertTrue) and adds specific checks on javaScript objects/variables (see javadoc for reference). It also provides the means to execute your own validations, wether they receive one or tow parameters.
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.
Extend JSTestCase as you would normally do with any TestCase. JsTestCase will take care of the lifecycle of its JsTester.
public class MyTestCase extends JsTestCase { public MyTestCase( String testName ){ super( testName ); } }
You can insert your own validations any time, just call the 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") ); }
Evaling the code will run your javaScript code in the rhino engine, affecting the global
scope as intended (that its what we want to assert). Call 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.jsassertNotNull("myobject"); } public void testInlineJs(){ eval("var helloFunc = function(){ return 'hello world'; };");assertIsFunction("helloFunc"); } }