test: enhance handleNewAddRequest test with ArgumentCaptor and explicit assertions

This commit is contained in:
2025-02-09 20:18:26 +03:00
parent e307278e4b
commit b307543228
@@ -34,21 +34,36 @@ class BookCreationServiceImplTest {
@BeforeEach
void setUp() {
testUser = new BotUser(1L, "testuser");
testUser = new BotUser();
testUser.setId(1L); // Явная установка ID
testUser.setUsername("testuser");
testCache = new BookCreationCache();
}
@Test
void handleNewAddRequest_ShouldInitializeCache() {
when(bookCacheService.updateCache(anyLong(), any(BookCreationCache.class)))
// Используем ArgumentCaptor для захвата аргументов
ArgumentCaptor<Long> userIdCaptor = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<BookCreationCache> cacheCaptor = ArgumentCaptor.forClass(BookCreationCache.class);
// Мокируем вызов с конкретными аргументами
when(bookCacheService.updateCache(eq(1L), any(BookCreationCache.class)))
.thenReturn(testCache);
bookCreationService.handleNewAddRequest(testUser, ADD_BOOK);
// Проверяем аргументы вызова
verify(bookCacheService).updateCache(userIdCaptor.capture(), cacheCaptor.capture());
verify(bookCacheService).updateCache(eq(1L), any(BookCreationCache.class));
verify(telegramClientService).sendMessageWithMarkdown(eq(testUser),
contains("Введите название книги"), isNull());
// Убеждаемся в корректности параметров
assertEquals(1L, userIdCaptor.getValue());
assertEquals(BookCreationCache.CreationState.ADDING_TITLE, cacheCaptor.getValue().getCreationState());
verify(telegramClientService).sendMessageWithMarkdown(
eq(testUser),
contains("Введите название книги"),
isNull()
);
}
@Test