Eclipse插件开发 - 单元测试e4部件

时间:2017-10-14 19:40:43

标签: unit-testing eclipse-plugin e4

我正在尝试使用E4样式视图测试Eclipse插件。我在JUnit插件测试运行配置中创建了一个测试类。但是我甚至都在努力打开这个观点。旧的e3方式是使用以下代码段打开视图:

PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().showView("some view id")

在e4中,我们可以使用EPartServicedocumentation)代替。但是,我不确定如何访问它。试图注入它不起作用。这就是我的尝试:

public class MyTest {

@Inject EPartService partService;

@Test
public void testOne() {
// partService is still null here...

我还尝试将@PostConstruct注释添加到测试方法中,没有运气。不应该这样做,因为它是作为JUnit插件测试运行的,我假设它有一个Eclipse上下文,部件服务准备好了吗?

根据this文章,还应该可以创建一个Display类和一个关联的Shell,可以将其传递给视图的createPartControl方法。我也在测试方法中试过这个:

    Display display = Display.getCurrent();
    Shell shell = new Shell(display);
    IEclipseContext context = EclipseContextFactory.create();
    IPartService partService = mock(IPartService.class);
    context.set(IPartService.class, partService);
    context.set(Composite.class, shell);
    view = ContextInjectionFactory.make(InspView.class, context);
    shell.open();

我需要IPartService(不要与EPartService混淆......)因为视图需要它。这实际上有效。但是,正如您所看到的,我需要模拟IPartService并将其传递给Eclipse上下文。这不能给我我想要的东西。相反,我想要一个Eclipse上下文,其中实例化了所有服务。这可能吗?哪种方法最正确?使用Display + Shell,还是EPartService?

谢谢!

更新: 可以使用以下方法打开零件:

EPartService partService = 
PlatformUI.getWorkbench().getService(EPartService.class);
partService.showPart(VIEW_ID, PartState.ACTIVATE);

这是一个好方法吗?

0 个答案:

没有答案
相关问题