LeaderController { @PutMapping public ResponseEntity<String> revokeLeadership() { if (this.context == null) { String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host); return ResponseEntity.badRequest().body(message); } this.context.yield(); String message = String.format("Leadership revoked for '%s'", this.host); return ResponseEntity.ok(message); } LeaderController(); @GetMapping String getInfo(); @PutMapping ResponseEntity<String> revokeLeadership(); @EventListener void handleEvent(OnGrantedEvent event); @EventListener void handleEvent(OnRevokedEvent event);  }
@Test public void shouldNotRevokeLeadershipIfNotLeader() { ResponseEntity<String> responseEntity = this.leaderController.revokeLeadership(); String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host); assertThat(responseEntity.getBody()).isEqualTo(message); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); verify(this.mockContext, times(0)).yield(); }