Created
October 5, 2017 07:50
-
-
Save pkuerzer/140528945182bde8caa1e64bacdf231f to your computer and use it in GitHub Desktop.
SpringWebMvcTest - Test Requestbody using @Valid and custom validation
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
@RestController | |
public class BalanceInquiryController { | |
@PostMapping(value = "/com/balanceInquiry") | |
public ResponseEntity<?> getBalanceInquiry(@Valid @RequestBody BalanceInquiryModel balanceInquiry, Errors errors) { | |
if (errors.hasErrors()) { | |
return new ResponseEntity<String>("Validation", HttpStatus.BAD_REQUEST); | |
} | |
//any stuff | |
return new ResponseEntity<BalanceInquiryResponse>(balanceInquiryResponse, HttpStatus.OK); | |
} | |
} |
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
@RunWith(SpringRunner.class) | |
@WebMvcTest(value = BalanceInquiryController.class, secure = false) | |
public class BalanceInquiryControllerTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@MockBean | |
private BalanceInquiryController balanceInquiryController; | |
@Test | |
public void testGetBalanceInquiry() throws Exception { | |
BalanceInquiryModel model = new BalanceInquiryModel(); | |
GiftCard card = new GiftCard(); | |
card.setCardNumber("1234454");//not valid. A valid cardnumber is: 1234567890123456789 | |
card.setPinNumber("1234"); | |
model.setGiftCard(card); | |
model.setMerchant(Merchant.ANY);//you need a Merchant enum - create one if you copy the code | |
RequestBuilder requestBuilder = MockMvcRequestBuilders | |
.post("/com/balanceInquiry") | |
.accept(MediaType.APPLICATION_JSON) | |
.content(stringify(model)) | |
.contentType(MediaType.APPLICATION_JSON); | |
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); | |
MockHttpServletResponse response = mvcResult.getResponse(); | |
assertEquals(HttpStatus.OK.value(), response.getStatus());//response should be 400 because of vailidation | |
} | |
private String stringify(Object object) throws JsonProcessingException { | |
return new ObjectMapper().writeValueAsString(object); | |
} | |
} |
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 class BalanceInquiryModel { | |
@Valid | |
@NotNull | |
private GiftCard giftCard; | |
@NotNull | |
private Merchant merchant; | |
public GiftCard getGiftCard() { | |
return giftCard; | |
} | |
public void setGiftCard(GiftCard giftCard) { | |
this.giftCard = giftCard; | |
} | |
public Merchant getMerchant() { | |
return merchant; | |
} | |
public void setMerchant(Merchant merchant) { | |
this.merchant = merchant; | |
} | |
} |
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
@Documented | |
@Retention(RUNTIME) | |
@Target({ FIELD, METHOD }) | |
@Constraint(validatedBy = CardNumberValidator.class) | |
public @interface CardNumberValidation { | |
String message() default "Invalid Cardnumber. Cardnumber should have a length of 19 digits."; | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
} |
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 class CardNumberValidator implements ConstraintValidator<CardNumberValidation, String> { | |
@Override | |
public void initialize(CardNumberValidation arg0) { | |
} | |
@Override | |
public boolean isValid(String cardnumber, ConstraintValidatorContext cxt) { | |
return cardnumber != null && cardnumber.matches("\\d{19}") && cardnumber.length() == 19; | |
} | |
} |
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
@Validated | |
public class GiftCard { | |
@CardNumberValidation | |
private String cardNumber; | |
@PinNumberValidation | |
private String pinNumber; | |
public String getCardNumber() { | |
return cardNumber; | |
} | |
public void setCardNumber(String cardNumber) { | |
this.cardNumber = cardNumber; | |
} | |
public String getPinNumber() { | |
return pinNumber; | |
} | |
public void setPinNumber(String pinNumber) { | |
this.pinNumber = pinNumber; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment