In this sprint challenge, you'll apply the core concepts from this sprint: dependency injection, test-driven development, and mocking to build a library service application. You'll demonstrate your ability to write loosely coupled, testable code using best practices.
// Example of a properly designed service with dependency injection public class LibraryService { private final BookRepository bookRepository; private final UserRepository userRepository; private final NotificationService notificationService; public LibraryService(BookRepository bookRepository, UserRepository userRepository, NotificationService notificationService) { this.bookRepository = bookRepository; this.userRepository = userRepository; this.notificationService = notificationService; } public boolean borrowBook(String userId, String bookId) { User user = userRepository.findById(userId); Book book = bookRepository.findById(bookId); if (user == null || book == null || !book.isAvailable()) { return false; } book.setAvailable(false); book.setBorrowedBy(userId); bookRepository.save(book); notificationService.sendBookBorrowedNotification(user.getEmail(), book.getTitle()); return true; } } // Example of a proper test with mocks @Test void shouldBorrowAvailableBook() { // Arrange String userId = "user123"; String bookId = "book456"; User testUser = new User(userId, "test@example.com"); Book testBook = new Book(bookId, "Test Book", true); BookRepository mockBookRepo = mock(BookRepository.class); UserRepository mockUserRepo = mock(UserRepository.class); NotificationService mockNotification = mock(NotificationService.class); when(mockUserRepo.findById(userId)).thenReturn(testUser); when(mockBookRepo.findById(bookId)).thenReturn(testBook); LibraryService service = new LibraryService(mockBookRepo, mockUserRepo, mockNotification); // Act boolean result = service.borrowBook(userId, bookId); // Assert assertTrue(result); assertFalse(testBook.isAvailable()); assertEquals(userId, testBook.getBorrowedBy()); verify(mockBookRepo).save(testBook); verify(mockNotification).sendBookBorrowedNotification(testUser.getEmail(), testBook.getTitle()); }