测试 Eclipse 4 RCP 应用程序。提供必要的对象

Test Eclipse 4 RCP Application. Provide necessary Objects

我正在开发 Eclipse 4 RCP 应用程序,我想测试我的部件的一些功能。 我有一个这样的测试 Class:

@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 期间创建了一个 DataBinding,这就是我 运行 进入 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 argument:Validation realm cannot be null
at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)
at org.eclipse.core.databinding.DataBindingContext.<init>(DataBindingContext.java:95)
at org.eclipse.core.databinding.DataBindingContext.<init>(DataBindingContext.java:82)
at de.uni_due.s3.jack.editor.parts.config.ConfigPart.addDataBinding(ConfigPart.java:350)
at de.uni_due.s3.jack.editor.parts.config.ConfigPart.postConstruct(ConfigPart.java:81)
at de.uni_due.s3.jack.editor.parts.config.ConfigPartTest.initUI(ConfigPartTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

对空构造函数 new DataBindingContext() 的调用委托给 this(Realm.getDefault())(请参阅 Eclipse 源代码)。这意味着您需要将某种存根领域设置为用于测试目的的默认领域。

你可以使用这个solution from the Eclipse Wiki。这是来自 Wiki 的复制粘贴(适用于您的设置)。我会考虑您是否真的需要在 @BeforeClass 中进行设置,或者如果 @Before 会更好。

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