void () { final MessageBusConnection connection = myProject.getMessageBus().connect(); final MyModuleListener moduleListener = new MyModuleListener(); connection.subscribe(ModuleListener.TOPIC, moduleListener); final ModuleManager moduleManager = ModuleManager.getInstance(myProject); final Module moduleA; final Module moduleB; Path dir = ProjectKt.getStateStore(myProject).getProjectBasePath(); { final ModifiableModuleModel modifiableModel = moduleManager.getModifiableModel(); moduleA = modifiableModel.newModule(dir.resolve("a.iml"), StdModuleTypes.JAVA.getId()); moduleB = modifiableModel.newModule(dir.resolve("b.iml"), StdModuleTypes.JAVA.getId()); assertEquals("Changes are not applied until commit", 0, moduleManager.getModules().length); //noinspection SSBasedInspection moduleListener.assertCorrectEvents(new String[0][]); ApplicationManager.getApplication().runWriteAction(modifiableModel::commit); } assertEquals(2, moduleManager.getModules().length); assertTrue(moduleManager.findModuleByName("a").equals(moduleA)); assertTrue(moduleManager.findModuleByName("b").equals(moduleB)); moduleListener.assertCorrectEvents(new String[][]{{"+a", "+b"}}); { final ModifiableModuleModel modifiableModel = moduleManager.getModifiableModel(); modifiableModel.disposeModule(moduleA); modifiableModel.disposeModule(moduleB); assertEquals("Changes are not applied until commit", 2, moduleManager.getModules().length); moduleListener.assertCorrectEvents(new String[][]{{"+a", "+b"}}); ApplicationManager.getApplication().runWriteAction(modifiableModel::commit); } assertEquals(0, moduleManager.getModules().length); moduleListener.assertCorrectEvents(new String[][]{{"+a", "+b"}, {"-a", "-b"}}); connection.disconnect(); }