public RunnableHolder getWriteActionHolder() { final RunnableHolder runnableHolder = new RunnableHolder(); doAnswer( invocation -> { runnableHolder.setRunnable((Runnable) invocation.getArguments()[0]); return null; }) .when(application) .runWriteAction(any(Runnable.class)); return runnableHolder; }
@Test public void testJavaFileIsEmpty() throws Exception { final PsiJavaFile psiJavaFile = mock(PsiJavaFile.class); final PsiClass toImport = mock(PsiClass.class); final PsiImportStatement psiImportStatement = mock(PsiImportStatement.class); final PsiImportList psiImportList = mock(PsiImportList.class); final RunnableHolder writeActionHolder = getWriteActionHolder(); when(psiJavaFile.getProject()).thenReturn(project); when(psiImportStatement.getParent()).thenReturn(psiImportList); when(psiElementFactory.createImportStatement(toImport)).thenReturn(psiImportStatement); addImportService.addImportStatement(psiJavaFile, toImport); assertNotNull(writeActionHolder.getRunnable()); verify(psiJavaFile, never()).add(any()); writeActionHolder.getRunnable().run(); verify(psiJavaFile).add(psiImportList); }