-
Notifications
You must be signed in to change notification settings - Fork 417
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
Conversation
Product Coverage Report
|
Cart Coverage Report
|
inside test/resources, root of product module or root of yas with the content:
I also tried to add the 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: https://java.testcontainers.org/features/reuse/ So, this is the first way to config test container for re-use.
And the result is good here, when I run: |
cartRepository = mock(CartRepository.class); | ||
cartItemRepository = mock(CartItemRepository.class); | ||
productService = mock(ProductService.class); | ||
cartService = new CartService(cartRepository, cartItemRepository, productService); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
Quality Gate passed for 'yas'Issues Measures |
Quality Gate passed for 'product'Issues Measures |
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); |
There was a problem hiding this comment.
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.
Update
Now, to build a specific module in local with maven, instead of normal use:
use this command to run and build all the necessary module: