Spring boot
Application Runner
@Slf4j
public class MyApp implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
String username = getValueOrDefault(args, "username", propertyUsername);
String password = getValueOrDefault(args, "password", propertyPassword);
String projectUrl = getValueOrDefault(args, "projectUrl", propertyProjectUrl);
log.info("Using username={}, password={}, projectUrl={}", username, password, projectUrl);
}
protected String getValueOrDefault(ApplicationArguments args, String commandLineKey, String defaultValue) {
if (args.containsOption(commandLineKey) && !args.getOptionValues(commandLineKey).isEmpty()) {
return args.getOptionValues(commandLineKey).get(0);
}
return defaultValue;
}
}
Test web service
@RunWith(SpringRunner.class)
@WebMvcTest(Controller.class)
public class ControllerUTest {
@Autowired private MockMvc mockMvc;
@MockBean private MoveService moveService;
@MockBean private StartService startService;
@Test
public void testMove() throws Exception {
when(moveService.move(any(BattleSnakeRequest.class)))
.thenReturn(MoveResponse.builder().move(MOVE.UP)
.build());
this.mockMvc
.perform(
post("/move")
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF-8")
.content(getContent("move-request.json"))
)
.andExpect(status().isOk())
.andExpect(content().json("{\"move\":\"up\"}"))
;
}
}
Last updated