Skip to content

feat: add comprehensive test coverage for Book and Category entities#14

Merged
JlusS merged 4 commits into
masterfrom
HW
Sep 17, 2025
Merged

feat: add comprehensive test coverage for Book and Category entities#14
JlusS merged 4 commits into
masterfrom
HW

Conversation

@JlusS

@JlusS JlusS commented Sep 13, 2025

Copy link
Copy Markdown
Owner
  • Added unit tests for BookRepository and CategoryRepository
  • 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

- 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

@Elena-Bruyako Elena-Bruyako left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply comments for all tests

// Given
BookDto bookDto = createBookDto();
Page<BookDto> page = new PageImpl<>(List.of(bookDto));
when(bookService.findAll(any(Pageable.class))).thenReturn(page);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't mock data for integration tests
fix other places


String responseContent = result.getResponse().getContentAsString();

Assertions.assertTrue(responseContent.contains("Test Book"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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\""));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never use numbers for naming

and return the corresponding BookDto with correct category IDs
""")
void save_withValidRequest_returnsBookDtoWithCategoryIds() {
CreateBookRequestDto requestDto = new CreateBookRequestDto();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add verify() for all unit test methods

@Elena-Bruyako Elena-Bruyako left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

@DisplayName("Get all books")
void findAll_WithUserRole_ShouldReturnPageOfBooks() throws Exception {
// Given
Category categoryProgramming = categoryRepository.save(createProgrammingCategory());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use sql scripts to insert test data into database

);

assertThat(books).hasSize(1);
assertThat(books.get(0).getTitle()).isEqualTo("Effective Java");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare content

BookDto.class
);
assertNotNull(actual);
assertEquals(savedBook.getTitle(), actual.getTitle());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare objects actual and expected
not by fields

assertNotNull(actual);
assertEquals(requestDto.getTitle(), actual.getTitle());
assertEquals(requestDto.getAuthor(), actual.getAuthor());
assertEquals(1, bookRepository.count());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

assertNotNull(books);
assertEquals(1, books.length);
assertEquals(book.getTitle(), books[0].getTitle());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add not valid test cases
compare content or expected and actual objects
don't compare by fields

Comment on lines +23 to +24
@Transactional
@Testcontainers

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need

@Transactional
@Testcontainers


List<Book> actual = bookRepository.findAllByCategories_Id(savedCategory.getId());

Assertions.assertEquals(1, actual.size());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use static import

import org.springframework.test.web.servlet.MvcResult;

@SpringBootTest
@AutoConfigureMockMvc

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@AutoConfigureMockMvc


@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need profiles here?

@@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need profiles?

@JlusS JlusS requested a review from Elena-Bruyako September 16, 2025 14:48
@JlusS JlusS merged commit 022b525 into master Sep 17, 2025
2 checks passed
@JlusS JlusS deleted the HW branch September 17, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants