### Question:
ConfigMigration { public Map<String, Object> migrate(Map<String, Object> map) { int foundConfigVersion = (int) ((Map<String, Object>) map.get("main")).get("configVersion"); if (foundConfigVersion > expectedConfigVersion) { throw new RuntimeException("The existing config file has version " + foundConfigVersion + " but the program expects version " + expectedConfigVersion + ". You might be trying to use an older code base with settings created by a newer version of the program"); } for (ConfigMigrationStep step : steps) { if (foundConfigVersion <= step.forVersion()) { logger.info("Migrating config from version {}", step.forVersion()); map = step.migrate(map); foundConfigVersion = step.forVersion() + 1; } ((Map<String, Object>) map.get("main")).put("configVersion", foundConfigVersion); } if (foundConfigVersion != expectedConfigVersion) { throw new RuntimeException(String.format("Expected the config after migration to be at version %d but it's at version %d", expectedConfigVersion, foundConfigVersion)); } return map; } ConfigMigration(); Map<String, Object> migrate(Map<String, Object> map);  }### Answer:
@Test public void shouldMigrate() { BaseConfig input = new BaseConfig(); input.getMain().setConfigVersion(1); BaseConfig afterMigration = new BaseConfig(); afterMigration.getMain().setConfigVersion(2); Map<String, Object> map = objectMapper.convertValue(input, typeRef); when(configMigrationStepMock.forVersion()).thenReturn(1); when(configMigrationStepMock.migrate(any())).thenReturn(map); testee.steps = Arrays.asList(configMigrationStepMock); testee.expectedConfigVersion = 2; Map<String, Object> result = testee.migrate(map); input = objectMapper.convertValue(result, BaseConfig.class); verify(configMigrationStepMock).migrate(map); assertThat(input.getMain().getConfigVersion()).isEqualTo(2); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfWrongConfigVersionAfterMigration() { BaseConfig input = new BaseConfig(); input.getMain().setConfigVersion(1); Map<String, Object> map = objectMapper.convertValue(input, typeRef); testee.steps = Collections.emptyList(); testee.expectedConfigVersion = 2; testee.migrate(map); }