Conversation
- Implemented service layer tests for BookService and CategoryService - Added controller tests for BookController and CategoryController with security mocking - Achieved over 50% test coverage for targeted classes - Included test coverage screenshot in PR description - Configured test environment with H2 database and security mocks - Added necessary test dependencies and configuration files Tests cover: - CRUD operations for both Book and Category entities - Security constraints and role-based access control - Pagination and search functionality - Validation and error handling scenarios
| // Given | ||
| BookDto bookDto = createBookDto(); | ||
| Page<BookDto> page = new PageImpl<>(List.of(bookDto)); | ||
| when(bookService.findAll(any(Pageable.class))).thenReturn(page); |
There was a problem hiding this comment.
don't mock data for integration tests
fix other places
|
|
||
| String responseContent = result.getResponse().getContentAsString(); | ||
|
|
||
| Assertions.assertTrue(responseContent.contains("Test Book")); |
There was a problem hiding this comment.
use static imports for all places
| Assertions.assertTrue(responseContent.contains("Test Author")); | ||
| Assertions.assertTrue(responseContent.contains("29.99")); | ||
|
|
||
| Assertions.assertTrue(responseContent.contains("\"title\":\"Test Book\"")); |
There was a problem hiding this comment.
compare objects or content
don't compare by fields
same comment for all places
| book.setPrice(requestDto.getPrice()); | ||
| book.setIsbn(requestDto.getIsbn()); | ||
|
|
||
| Category category1 = new Category(); |
| and return the corresponding BookDto with correct category IDs | ||
| """) | ||
| void save_withValidRequest_returnsBookDtoWithCategoryIds() { | ||
| CreateBookRequestDto requestDto = new CreateBookRequestDto(); |
There was a problem hiding this comment.
create TestUtil class and initialise all test data there
don't forget to put this class in util package
|
|
||
| BookDto actualDto = bookService.save(requestDto); | ||
| Assertions.assertEquals(expectedDto, actualDto); | ||
| } |
There was a problem hiding this comment.
add verify() for all unit test methods
| @DisplayName("Get all books") | ||
| void findAll_WithUserRole_ShouldReturnPageOfBooks() throws Exception { | ||
| // Given | ||
| Category categoryProgramming = categoryRepository.save(createProgrammingCategory()); |
There was a problem hiding this comment.
use sql scripts to insert test data into database
| ); | ||
|
|
||
| assertThat(books).hasSize(1); | ||
| assertThat(books.get(0).getTitle()).isEqualTo("Effective Java"); |
| BookDto.class | ||
| ); | ||
| assertNotNull(actual); | ||
| assertEquals(savedBook.getTitle(), actual.getTitle()); |
There was a problem hiding this comment.
compare objects actual and expected
not by fields
| assertNotNull(actual); | ||
| assertEquals(requestDto.getTitle(), actual.getTitle()); | ||
| assertEquals(requestDto.getAuthor(), actual.getAuthor()); | ||
| assertEquals(1, bookRepository.count()); |
| assertNotNull(books); | ||
| assertEquals(1, books.length); | ||
| assertEquals(book.getTitle(), books[0].getTitle()); | ||
| } |
There was a problem hiding this comment.
add not valid test cases
compare content or expected and actual objects
don't compare by fields
| @Transactional | ||
| @Testcontainers |
There was a problem hiding this comment.
Why do you need
@Transactional
@Testcontainers
|
|
||
| List<Book> actual = bookRepository.findAllByCategories_Id(savedCategory.getId()); | ||
|
|
||
| Assertions.assertEquals(1, actual.size()); |
| import org.springframework.test.web.servlet.MvcResult; | ||
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc |
There was a problem hiding this comment.
| @AutoConfigureMockMvc |
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| @ActiveProfiles("test") |
| @@ -1,9 +1,6 @@ | |||
| spring.datasource.url=jdbc:h2:mem:testdb | |||
| spring.datasource.driverClassName=org.h2.Driver | |||
| spring.datasource.url=jdbc:tc:mysql:8.0.37:///mydb | |||
Tests cover: