I use Rhino to embed JavaScript in my Java code. In JavaScript it failed to instantiate a subclass of TestCase, which is a class from JUnit 3, but it does succeed to instantiate a subclass of Assert, which is the superclass of TestCase.
Below is some code to show this:
package com.example.test;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.mozilla.javascript.*;
public class MyFirstJavaProgram {
public static void main(String[] args) {
try {
Context cx = Context.enter();
Scriptable scope = new ImporterTopLevel(cx);
String js = new String();
js += "importPackage(Packages.com.example.test);";
js += "importClass(java.lang.System);";
js += "var testCaseClass = new TestCaseClass();";
js += "System.out.println(\"testCaseClass: \" + testCaseClass);";
js += "var assertClass = new AssertClass();";
js += "System.out.println(\"assertClass: \" + assertClass);";
cx.evaluateString(scope, js, "<cmd>", 1, null);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class TestCaseClass extends TestCase {
public TestCaseClass() {}
}
class AssertClass extends Assert {
public AssertClass() {}
}
And the output was:
testCaseClass: null(com.example.test.TestCaseClass)
assertClass: com.example.test.AssertClass@17943a4
Please don't ask me why I am doing this, let's just say I have to.
Thank you for your patience.
The string you're seeing:
null(com.example.test.TestCaseClass)
is exactly new TestCase().toString()
, the string representation of an unnamed test; this code is working.
Try:
class TestCaseClass extends TestCase {
public TestCaseClass() {
super("Named Test");
}
}
to make this clearer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.