[英]why can't I instantiate a subclass of TestCase using JavaScript on Rhino
I use Rhino to embed JavaScript in my Java code. 我使用Rhino将JavaScript嵌入我的Java代码中。 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.
在JavaScript中,它无法实例化TestCase的子类,它是JUnit 3的一个类,但确实可以实例化Assert的子类,后者是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; 完全是
new TestCase().toString()
,它是未命名测试的字符串表示形式; this code is working. 该代码有效。
Try: 尝试:
class TestCaseClass extends TestCase {
public TestCaseClass() {
super("Named Test");
}
}
to make this clearer. 使其更清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.