テストスクリプトがこんな感じ。
importPackage(Packages.foo);function assertEquals(executed, actual) {
Packages.junit.framework.Assert.assertEquals(executed, actual);
}var calc = new Calc();
assertEquals(3, calc.add(2, 2));
テストランナーとテストケースがこんな感じ。
public class RhinoTestRunner extends TestRunner {public static void main(String args[]) {
RhinoTestRunner runner = new RhinoTestRunner();try {
TestResult r = runner.start(args);if (!r.wasSuccessful())
System.exit(FAILURE_EXIT);System.exit(SUCCESS_EXIT);
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(EXCEPTION_EXIT);
}
}public Test getTest(String scriptName) {
clearStatus();
TestSuite suite = new TestSuite();
suite.addTest(new RhinoTestCase(scriptName));
return suite;
}}
public class RhinoTestCase extends TestCase {private String scriptName = null;
public RhinoTestCase(String scriptName) {
super("testScript");
this.scriptName = scriptName;
}public void testScript() throws Exception {
setName(scriptName);FileReader fin = new FileReader(scriptName);
BufferedReader in = new BufferedReader(fin);try {
runScript(in, scriptName);
} finally {
in.close();
fin.close();
}
}public void runScript(Reader in, String filename) throws IOException {
Context cx = Context.enter();try {
Script script = cx.compileReader(in, filename, 1, null);
Global global = new Global();
ShellContextFactory factory = new ShellContextFactory();
global.init(factory);
Object result = script.exec(cx, global);if (result != Context.getUndefinedValue())
System.err.println(Context.toString(result));
} finally {
Context.exit();
}
}}