I made a very simple rest app (palindrome game) that is supposed to show the user different information depending on what word he/she entered as a path variable. I already tested it via Postman and everything went well but when I wrote actual tests, they showed not the results I need.
So, I have the following PostMapping method. If the user enters a palindrome as a path variable "string" he is supposed to see how many points he/she got. If the word was already used by another user - "Already in use. Try again". And if the word is not a palindrome, then he/she will see the message "Not a palindrome".
@PostMapping("/play/{username}/{string}")
public ResponseEntity<String> enterPalindrome(@PathVariable("username") String username, @PathVariable("string") String string) {
/*check if the word is a palindrome*/
if (palindromeService.isPalindrome(string)) {
/*check if the word was already used by another user*/
if (palindromeService.isUniquePalindrome(palindromesTable, string)) {
palindromesTable.put(string, username);
} else {
return new ResponseEntity<>("Already in use. Try again", HttpStatus.OK);
}
/*add the username and his/her points into Map<String, Integer> scoreTable */
userService.registerUser(scoreTable, username);
int currentScore = scoreTable.get(username);
int totalScore = userService.calculateScore(currentScore,string);
scoreTable.put(username, totalScore);
return new ResponseEntity<>("Good job " + username + "! You earned " + string.length() / 2 + " points. Your score is" + totalScore, HttpStatus.OK);
}
return new ResponseEntity<>("Not a palindrome. Try again", HttpStatus.OK);
}
This is the test I made:
@Test
public void returnTrue_ifSuccessfullyEnterPalindrome() throws Exception {
MvcResult mvcResult = mockMvc.perform(post("/palindrome/play/{username}/{string}", "Andrew", "madam"))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("Not a palindrome. Try again", mvcResult.getResponse().getContentAsString());
As I thought, it is supposed to return false because the word "madam" is a palindrome but it returns true. And in fact it returns true regardless of what word I use.
Aucun commentaire:
Enregistrer un commentaire