Created
April 8, 2019 22:45
-
-
Save singun/590dded3ba054b81f4c92130104b8059 to your computer and use it in GitHub Desktop.
ArgumentCaptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String processFindForm(Owner owner, BindingResult result, Model model){ | |
// allow parameterless GET request for /owners to return all records | |
if (owner.getLastName() == null) { | |
owner.setLastName(""); // empty string signifies broadest possible search | |
} | |
// find owners by last name | |
List<Owner> results = ownerService.findAllByLastNameLike("%"+ owner.getLastName() + "%"); | |
if (results.isEmpty()) { | |
// no owners found | |
result.rejectValue("lastName", "notFound", "not found"); | |
return "owners/findOwners"; | |
} else if (results.size() == 1) { | |
// 1 owner found | |
owner = results.get(0); | |
return "redirect:/owners/" + owner.getId(); | |
} else { | |
// multiple owners found | |
model.addAttribute("selections", results); | |
return "owners/ownersList"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
void testProcessFindForm() { | |
// given | |
Owner owner = new Owner(1l, "Shin", "Daewook"); | |
List<Owner> owners = new ArrayList<>(); | |
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); | |
given(ownerService.findAllByLastNameLike(captor.capture())).willReturn(owners); | |
// when | |
String result = ownerController.processFindForm(owner, bindingResult, null); | |
// then | |
assertThat("%Daewook%").isEqualToIgnoringCase(captor.getValue()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Captor | |
ArgumentCaptor<String> stringArgumentCaptor; | |
@Test | |
void testProcessFindFormAnnotation() { | |
// given | |
Owner owner = new Owner(1l, "Shin", "Daewook"); | |
List<Owner> owners = new ArrayList<>(); | |
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); | |
given(ownerService.findAllByLastNameLike(stringArgumentCaptor.capture())).willReturn(owners); | |
// when | |
String result = ownerController.processFindForm(owner, bindingResult, null); | |
// then | |
assertThat("%Daewook%").isEqualToIgnoringCase(stringArgumentCaptor.getValue()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Argument Matcher와 다른 점은 따로 클래스를 만들 필요가 없다는 점, 그리고 verification에만 사용할 수 있다는 점이다.