Junit 如何在RCP应用程序中使用mockito测试异常?

Junit 如何在RCP应用程序中使用mockito测试异常?,junit,mockito,swt,rcp,Junit,Mockito,Swt,Rcp,我的向导类的performFinish()方法中有以下代码: public boolean performFinish() { try { getContainer().run(true, false, changeArtifactRunnable()); } catch (InvocationTargetException | InterruptedException e) { LoggerClass.logException(e);

我的向导类的
performFinish()
方法中有以下代码:

public boolean performFinish() {

    try {
      getContainer().run(true, false, changeArtifactRunnable());
    }
    catch (InvocationTargetException | InterruptedException e) {
      LoggerClass.logException(e);
    }
我想使用Mockito测试
InvocationTargetException
InterruptedException
的异常。 在上面的代码中,
getContainer()
方法来自
org.eclipse.jface.wizard.wizard
类和

public void run(boolean fork, boolean cancelable,
            IRunnableWithProgress runnable) throws InvocationTargetException,
            InterruptedException;
方法来自
org.eclipse.jface.operation.irunablecontext


如何在
performFinish()
方法中测试这两个异常?

您可以使用expected关键字进行测试。例如:

@Test(expected = InvocationTargetException.class)
public void testInvocationTargetException()    {
    \\Invoke the method to be tested under the conditions, such that InvocationTargetException is thrown by it. No need of any assert statements
}
=========================================================================== 编辑:

您可以创建EditArtifactWizard类的Spy并模拟getContainerMethod的行为


请原谅我的打字错误或编译错误,因为我没有使用任何编辑器

请告诉我如何测试上述场景,因为在我的代码中,run()方法引发了performFinish()方法中存在的异常。你能分享整个类的内容吗performFinish()方法编辑了我的答案,看看是否有帮助谢谢你的帮助。getContainer()正在调用run()方法。run()方法是inturn引发异常,但不是getContainer()方法。请您帮助我如何调用run()方法。在这种情况下,请仔细阅读向导类的文档,查看IRunnableContext和向导类之间的关系,并相应地模拟run方法的行为。
    @RunWith(MockitoJUnitRunner.class)
    public class EditArtifactWizardTest    {
        @Spy 
        //use correct constructor of EditArtifactWizard 
        private EditArtifactWizard editArtifactWizardSpy=Mockito.spy(new EditArtifactWizard ());
           @Test(expected = InvocationTargetException.class)
        public void testInvocationTargetException()    {
            \\Invoke the method to be tested under the conditions, such that InvocationTargetException is thrown by it. No need of any assert statements
            Mockito.when(editArtifactWizardSpy.getContainer()).thenThrow(InvocationTargetException.class);
            editArtifactWizardSpy.performFinish();
        }
    }