gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:42:07 25 4
gpt4 key购买 nike

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

@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$1.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() 的调用委托(delegate)给 this(Realm.getDefault()) (请参阅 Eclipse 源代码)。这意味着您需要将某种 stub 领域设置为默认值以用于测试目的。

您可以使用这个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();
}

关于java - 测试 Eclipse 4 RCP 应用程序。提供必要的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30070707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com