WorkbenchPage.busyShowView 中的 NullPointerException

NullPointerException in WorkbenchPage.busyShowView

我在我的 Eclipse 插件 (4.5.2) 中正常注册了一个简单的视图,当我使用该插件启动 Eclipse 实例时它可以正常工作。它仍然适用于相应的测试用例,它有以下方法:

@Before
public void setUp() throws Exception {
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    for (IViewReference viewReference : activePage.getViewReferences()) {
        activePage.hideView(viewReference);
    }
    activePage.showView("org.acme.MyView");
}

然而,当我 运行 使用 Tycho(0.22、0.24 或 0.25)进行相同的测试时,我得到以下异常:

java.lang.NullPointerException: null
    at org.eclipse.ui.internal.WorkbenchPage.busyShowView(WorkbenchPage.java:1271)
    at org.eclipse.ui.internal.WorkbenchPage.run(WorkbenchPage.java:4238)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
    at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage.java:4234)
    at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage.java:4214)
    at org.acme.MyViewTest.setUp(MyViewTest.java:39)

第谷代码很简单:

<plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>tycho-surefire-plugin</artifactId>
    <configuration>
        <providerHint>junit4</providerHint>
        <useUIHarness>true</useUIHarness>
    </configuration>
</plugin>

我找到了 this bug 和其他几个,但我没有找到任何可以解释为什么它只会在第谷失败的原因。我找不到任何关于如何解决这个问题的信息。

那我做错了什么?我该如何解决?

我的猜测是,您的测试运行时缺少一些完整的 Eclipse workbench 所需的东西,而 PDE 添加了一些东西,但 Tycho 没有。 (默认情况下,Tycho 仅将 eclipse-test-plugin 的(传递)依赖项添加到测试运行时。)

尝试将以下内容添加到您的 tycho-surefire-plugin 执行中:

<configuration>
  <!-- ... ->
  <dependencies>
    <dependency>
      <artifactId>org.eclipse.e4.rcp</artifactId>
      <type>eclipse-feature</type>
    </dependency>
  </dependencies>
</configuration>

这应该将许多插件拉入您的测试运行时,否则这些插件可能不存在(例如 org.eclipse.e4.core.di,测试通常不直接或间接依赖)。

当然,只有当 org.eclipse.e4.rcp 功能是您的目标平台的一部分时,以上内容才有效。