Testing using JsTester

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:

1. Inline JsTester on your TestCase

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(); 
   }
}

2. Initialize JsTester with your own validators (optional)

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"));
   }

3. Eval the javaScript code

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.

4. Assert the javaScript code

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.

Full Example

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");
   } 
}

Testing using JsTestCase

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.

1. Extend the JsTestCase class

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 );
   }
}

2. Initialize your own validators (optional)

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") );
   }

3. Eval the javaScript code

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.

4. Assert the javaScript code

Assert your code as previously explained. The only change is that JsTestCase.assertEquals will not work on your javaScript code, you must use assertExpressionEquals().

Full Example

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");
   } 
}