void () { WriteCommandAction.writeCommandAction(getProject()).run(() -> { final Document doc = new DocumentImpl(""); final StringBuilder b = new StringBuilder(); doc.addDocumentListener(new DocumentListener() { @Override public void beforeDocumentChange(@NotNull DocumentEvent e) { b.append("before1 "); } @Override public void documentChanged(@NotNull DocumentEvent e) { b.append("after1 "); } }); doc.addDocumentListener(new DocumentListener() { @Override public void beforeDocumentChange(@NotNull DocumentEvent event) { doc.removeDocumentListener(this); } }); doc.addDocumentListener(new DocumentListener() { @Override public void documentChanged(@NotNull DocumentEvent e) { doc.removeDocumentListener(this); } }); doc.addDocumentListener(new DocumentListener() { @Override public void beforeDocumentChange(@NotNull DocumentEvent e) { b.append("before2 "); } @Override public void documentChanged(@NotNull DocumentEvent e) { b.append("after2 "); } }); doc.setText("foo"); assertEquals("before2 before1 after1 after2 ", b.toString()); }); }