Spring Boot, Mockito, Unit Testing, Junit 5, Rest Api.
when(this.booksRepository.findById(anyString())).thenReturn(Optional.of(savedBooks)); Books bookWithId = null; try { bookWithId = this.bookService.findById("testid-123"); assertThat(bookWithId.getId()).isNotNull(); } catch (Exception e) { assertThat(e).isInstanceOf(RuntimeException.class); } }