void () { configureFromFileText("a.java", "class Foo {\n" + " void foo(){\n" + " ArrayList \n" + //note whitespace after " }\n" + "}"); PsiClass aClass = ((PsiJavaFile)getFile()).getClasses()[0]; PsiCodeBlock body = aClass.getMethods()[0].getBody(); PsiReferenceExpression expression = (PsiReferenceExpression)((PsiExpressionStatementImpl)body.getStatements()[0]).getExpression(); PsiClass list = getJavaFacade().findClass("java.util.ArrayList", GlobalSearchScope.allScope(getProject())); ApplicationManager.getApplication().runWriteAction(() -> { PsiElement newElement = expression.bindToElement(list); CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newElement); }); assertEquals(""" import java.util.ArrayList; class Foo { void foo(){ ArrayList\s } }""", getFile().getText()); }