Java 从集成测试调用处理程序

Java 从集成测试调用处理程序,java,eclipse-plugin,Java,Eclipse Plugin,我知道我有史以来最奇特的用例,但我真的很喜欢测试,因此我想测试IHandler是否按照我认为应该的方式工作。问题是我不知道如何以编程方式调用它: IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() IWorkbenchWindow site = workbenchPage.getWorkbenchWindow(); Command command =

我知道我有史以来最奇特的用例,但我真的很喜欢测试,因此我想测试
IHandler
是否按照我认为应该的方式工作。问题是我不知道如何以编程方式调用它:

IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
IWorkbenchWindow site = workbenchPage.getWorkbenchWindow();
Command command = ((ICommandService) site.getService(ICommandService.class)).getCommand(commandId);

final IHandlerService service = (IHandlerService) site.getService(IHandlerService.class);
final Event trigger = new Event(); // or trigger = null
ExecutionEvent executionEvent = service.createExecutionEvent(command, trigger);
command.executeWithChecks(executionEvent);
处理程序被调用,但是
ExecutionEvent
没有正确填充(例如,
HandlerUtil.getActiveEditor(ExecutionEvent)
即使我刚刚打开了一个编辑器,也返回null)


如何以正确的方式编程调用
IHandler

我也非常喜欢测试:-)但是,我通常会手动为评估上下文配备处理程序执行所需的变量

例如:

EvaluationContext context = new EvaluationContext( null, new Object() )
context.addVariable( ISources.ACTIVE_PART_NAME, activePart );
context.addVariable( ISources.ACTIVE_CURRENT_SELECTION_NAME, selection );
// add whatever is used by the handler...

Map<String, String> parameters = new HashMap<>();
ExecutionEvent event = new ExecutionEvent( command, parameters, null, context );

command.executeWithChecks( event );
EvaluationContext=new EvaluationContext(null,new Object())
context.addVariable(isosources.ACTIVE\u PART\u NAME,activePart);
context.addVariable(ISources.ACTIVE\当前\选择\名称,选择);
//添加处理程序使用的任何内容。。。
映射参数=新的HashMap();
ExecutionEvent=新的ExecutionEvent(命令、参数、null、上下文);
command.executeWithChecks(事件);

让我知道这种方法对您是否可行。否则,我可能能够找出如何获得测试助手代码的真实评估上下文。

您显然可以从
IHandlerService.getCurrentState()
中获得
IEvaluationContext
。。。或者至少现在我的测试有效了。