Java 测试Eclipse4RCP应用程序。提供必要的物品
我正在开发一个Eclipse4RCP应用程序,我想测试我的部件的一些功能。 我有一个这样的测试类:Java 测试Eclipse4RCP应用程序。提供必要的物品,java,eclipse,eclipse-rcp,Java,Eclipse,Eclipse Rcp,我正在开发一个Eclipse4RCP应用程序,我想测试我的部件的一些功能。 我有一个这样的测试类: @BeforeClass public static void initUI() { display = new Display(); shell = new Shell(display); configPart = new ConfigPart(); configPart.postConstruct(shell); } @Test public void te
@BeforeClass
public static void initUI() {
display = new Display();
shell = new Shell(display);
configPart = new ConfigPart();
configPart.postConstruct(shell);
}
@Test
public void testConfigPart() {
String testText = "TitleText";
configPart.title.setText(testText);
assertEquals(testText, ConfigHandler.getInstance().getInternalConfig()
.getTitle());
}
在创建ConfigPart的过程中,会创建一个数据绑定,这就是我遇到AssertionFailedException的地方。声明如下:
DataBindingContext ctx = new DataBindingContext();
有没有办法避免这种情况,或者有没有其他方法来测试E4应用程序
编辑:
引发异常的语句:
public DataBindingContext(Realm validationRealm) {
Assert.isNotNull(validationRealm, "Validation realm cannot be null");
public static void isNotNull(Object object, String message) {
if (object == null) throw new AssertionFailedException("null argument:" + message);
堆栈跟踪:
org.eclipse.core.runtime.AssertionFailedException:null参数:验证域不能为null
位于org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)
位于org.eclipse.core.databinding.DataBindingContext。(DataBindingContext.java:95)
位于org.eclipse.core.databinding.DataBindingContext。(DataBindingContext.java:82)
位于de.uni_due.s3.jack.editor.parts.config.ConfigPart.addDataBinding(ConfigPart.java:350)
位于de.uni_due.s3.jack.editor.parts.config.ConfigPart.postConstruct(ConfigPart.java:81)
在de.uni_due.s3.jack.editor.parts.config.ConfigPartTest.initUI(ConfigPartTest.java:28)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:47)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:44)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
位于org.junit.internal.runners.statements.runafter.evaluate(runafter.java:27)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:309)
位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
对空构造函数的调用新建DataBindingContext()
将委托给此(Realm.getDefault())
(请参阅Eclipse源代码)。这意味着您需要将某种类型的存根域设置为测试目的的默认值
你可以用这个。这是Wiki的复制粘贴(适合您的设置)。我会考虑你是否真的需要在课前进行设置,或者在课前进行设置会更好
public class DefaultRealm extends Realm {
private Realm previousRealm;
public DefaultRealm() {
previousRealm = super.setDefault(this);
}
/**
* @return always returns true
*/
public boolean isCurrent() {
return true;
}
protected void syncExec(Runnable runnable) {
runnable.run();
}
/**
* @throws UnsupportedOperationException
*/
public void asyncExec(Runnable runnable) {
throw new UnsupportedOperationException("asyncExec is unsupported");
}
/**
* Removes the realm from being the current and sets the previous realm to the default.
*/
public void dispose() {
if (getDefault() == this) {
setDefault(previousRealm);
}
}
}
测试代码:
private static DefaultRealm realm;
@BeforeClass
public static void initUI() {
display = new Display();
shell = new Shell(display);
realm = new DefaultRealm();
configPart = new ConfigPart();
configPart.postConstruct(shell);
}
@AfterClass
public static void tearDownUI() {
realm.dispose();
}
你不能像这样测试e4部件,因为你不会得到任何注入,e4也不会被正确初始化。好吧,所以没有办法从代码中自动运行这样的测试?如果你想测试UI,你可以使用这个有用的链接,一定有办法,因为Eclipse本身运行很多测试,但是我不知道他们使用的是什么,因为
验证域不能为null,所以断言失败了吗?