void () { Project project = getProject(); PsiExpressionCodeFragment fragment = JavaCodeFragmentFactory.getInstance(project).createExpressionCodeFragment("a", null, null, true); Runnable writeAction = () -> ApplicationManager.getApplication().runWriteAction(() -> { Document document = PsiDocumentManager.getInstance(project).getDocument(fragment); document.insertString(1, "b"); PsiDocumentManager.getInstance(project).commitAllDocuments(); assertEquals("ab", fragment.getText()); assertEquals("ab", fragment.getExpression().getText()); //noinspection UnusedAssignment document = null; }); CommandProcessor.getInstance().executeCommand(project, writeAction, "", ""); GCUtil.tryGcSoftlyReachableObjects(); assertEquals("ab", PsiDocumentManager.getInstance(project).getDocument(fragment).getText()); }