有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java测试Eclipse4RCP应用程序。提供必要的物品

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

@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 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)

共 (1) 个答案

  1. # 1 楼答案

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

    你可以用这个solution from the Eclipse 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();
    }