I have a Spring REST Api that adds users to a db:
@PostMapping(value = "/adduser", produces = "application/json")
public ResponseEntity<?> addUser(@RequestBody User user){...}
Now I'm creating it's unit test where I placed this code:
ResponseEntity<?> user = userController.addUser(userTest);
The addUser method will either return a ResponseEntity<User>
containing the json of the added user (when it's added successfully) or return a ResponseEntity<String>
with an error message when an error occurred.
Now, how can I check which of those two was actually returned?
I want to use that information to create two Assert tests. One where userTest will be valid (expecting the User object as return) and one where userTest will be invalid input (expecting the String error message as return).
CodePudding user response:
you can just use instanceof operator, as follow :
if(user.getBody() instanceof String) {
// -- do something
}
if(user.getBody() instanceof User) {
// -- do this
}
CodePudding user response:
You should be able to use C#'s is
operator:
ResponseEntity<?> user = userController.addUser(userTest);
if (user is ResponseEntity<User>) {
// ...
} else {
// ...
}