Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#829 - Config common library and add test for product, cart,... for checking #1057

Merged
merged 9 commits into from
Sep 24, 2024

Conversation

khanhduzz
Copy link
Contributor

@khanhduzz khanhduzz commented Sep 23, 2024

Update

  • Update the version configuration in the common-library module to ensure compatibility with other modules
  • Enhance test coverage by add some integration tests into the product
  • Add some unit tests in the cart module to verify the functionality of the common library

Now, to build a specific module in local with maven, instead of normal use:

mvn clean install -f module

use this command to run and build all the necessary module:

 mvn clean install -pl module -am

@khanhduzz khanhduzz changed the title add test for product #829 - Add test for product Sep 23, 2024
Copy link

github-actions bot commented Sep 23, 2024

Product Coverage Report

Overall Project 72.11%
File Coverage
BrandService.java 97.25% 🍏

@khanhduzz khanhduzz changed the title #829 - Add test for product #829 - Config common library and add test for product, cart,... for checking Sep 23, 2024
Copy link

Cart Coverage Report

Overall Project 86.08% 🍏

There is no coverage information present for the Files changed

@khanhduzz khanhduzz linked an issue Sep 23, 2024 that may be closed by this pull request
@khanhduzz khanhduzz marked this pull request as ready for review September 23, 2024 08:41
@khanhduzz khanhduzz removed the request for review from VanCongBang September 23, 2024 09:02
@khanhduzz
Copy link
Contributor Author

khanhduzz commented Sep 24, 2024

  • After searching about testcontainer.reuse, I got some information:
    First, the testcontainers reuse need to create a file inside system, not inside project, I have tried create file with name like this:
testcontainers.properties
or
.testcontainers.properties

inside test/resources, root of product module or root of yas with the content:

testcontainers.reuse.enable=true

I also tried to add the TESTCONTAINERS_REUSE_ENABLE=true inside the .env file
But the log always show reuse was request but environment does not support:

Screenshot 2024-09-24 085630

Then, I tried to create a file .testcontainers.properties inside local system and run test, it works and re-use the test container, and I found this blog about test container:

Screenshot 2024-09-24 092537

https://blog.dkwr.de/development/2023-08-18-testcontainers-spring-boot-3/#:~:text=The%20setting.withReuse%28true%29%20keeps%20the%20container%20alive%20between%20test,~%2F.testcontainers.properties%20file%20and%20add%20the%20following%20line%3A%20testcontainers.reuse.enable%3Dtrue

https://java.testcontainers.org/features/reuse/

So, this is the first way to config test container for re-use.

  • The second way, I try to refactor the previous IT inside product module, add this config to all the IT class, to let spring re-use test container when run:
    image

And the result is good here, when I run: mvn clean install -pl product -am:

image

cartRepository = mock(CartRepository.class);
cartItemRepository = mock(CartItemRepository.class);
productService = mock(ProductService.class);
cartService = new CartService(cartRepository, cartItemRepository, productService);
Copy link
Contributor

Choose a reason for hiding this comment

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

Utilize @Injectmocks and @mock annotation instead of using mock() explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’ve updated the code to utilize @Injectmocks and @mock annotations. Please take a review, thank you.

}

@Test
void testAddToCart_ProductNotFound() {
Copy link
Contributor

@minhtridn2001 minhtridn2001 Sep 24, 2024

Choose a reason for hiding this comment

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

The method name should describe the condition and expected outcome as well.

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class CartServiceTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why there are no happy cases in this test class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This cart test class is designed solely to test the functionality of the common library. Since the cart module already has high code coverage, I’ve only added a few cases to trigger the pipeline

category.setName("Category");
categoryRepository.save(category);

productOne = Product.builder()
Copy link
Contributor

Choose a reason for hiding this comment

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

Extract test data object creations into another helper class to increase the readability of insertTestData()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’ve separated it. Please review it. Thank you!

productRelatedRepository.deleteAll();
productOptionRepository.deleteAll();
productImageRepository.deleteAll();
productOptionRepository.deleteAll();
Copy link
Contributor

Choose a reason for hiding this comment

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

productOptionRepository.deleteAll(); is called twice.


@Test
void test_getProductFeature_shouldReturnProductList() {
given(getRequestSpecification())
Copy link
Contributor

Choose a reason for hiding this comment

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

getRequestSpecification())
            .auth().oauth2(getAccessToken("admin", "admin"))

Extract this code into a method to avoid repetition code

private static final String PRODUCT_STOREFRONT_URL = "/v1/storefront/products";

@BeforeEach
public void insertTestData() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method creates a complex data setup. Consider creating specific test data for each test instead of setup everything for all tests.

Copy link
Contributor

@minhtridn2001 minhtridn2001 left a comment

Choose a reason for hiding this comment

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

Hi @khanhduzz,
I have some suggestions for enhancement. Please take a look.
Thanks!

@khanhduzz khanhduzz closed this Sep 24, 2024
@khanhduzz khanhduzz reopened this Sep 24, 2024
Copy link

sonarcloud bot commented Sep 24, 2024

Copy link

sonarcloud bot commented Sep 24, 2024


@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

@RunWith(MockitoJUnitRunner.class) already handle the mock initialization. You don't need to call openMocks manually.

@khanhtrand khanhtrand merged commit a5e2a39 into main Sep 24, 2024
15 checks passed
@khanhtrand khanhtrand deleted the #829-product-test branch September 24, 2024 07:45
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.

Add integration test for product services
5 participants