-
Notifications
You must be signed in to change notification settings - Fork 0
Fixes #1 - Merchant validation and Error handling #2
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
Open
Rd4dev
wants to merge
5
commits into
main
Choose a base branch
from
merchant-validation-error-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
merchant-service/src/test/java/com/mert/merchantservice/service/MerchantServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.mert.merchantservice.service; | ||
|
|
||
| import com.mert.merchantservice.exception.MerchantNotFoundException; | ||
| import com.mert.merchantservice.model.Merchant; | ||
| import com.mert.merchantservice.repository.MerchantRepository; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class MerchantServiceTest { | ||
| @Mock | ||
| private MerchantRepository merchantRepository; | ||
|
|
||
| @InjectMocks | ||
| private MerchantService merchantService; | ||
|
|
||
| @Test | ||
| void testMerchantService_validIdProvided_shouldDeleteMerchant() { | ||
| UUID id = UUID.randomUUID(); | ||
|
|
||
| Merchant merchant = new Merchant(); | ||
| merchant.setId(id); | ||
|
|
||
| when(merchantRepository.findById(id)).thenReturn(Optional.of(merchant)); | ||
| merchantService.deleteMerchant(id); | ||
| verify(merchantRepository).delete(merchant); | ||
| } | ||
|
|
||
| @Test | ||
| void testMerchantService_invalidIdProvided_shouldThrowMerchantNotFoundException() { | ||
| UUID id = UUID.randomUUID(); | ||
|
|
||
| when(merchantRepository.findById(id)).thenReturn(Optional.empty()); | ||
| assertThrows( | ||
| MerchantNotFoundException.class, | ||
| () -> merchantService.deleteMerchant(id) | ||
| ); | ||
| verify(merchantRepository, never()).delete(any()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Incomplete mock setup will cause test initialization failure.
MerchantService's constructor requires three dependencies:MerchantRepository,BillingServiceGrpcClient, andKafkaProducer(see lines 23-31 in MerchantService.java). OnlyMerchantRepositoryis mocked here. Without mocking the other two required dependencies,@InjectMockscannot instantiate the service, and these tests will fail during setup.🔧 Proposed fix
`@ExtendWith`(MockitoExtension.class) public class MerchantServiceTest { `@Mock` private MerchantRepository merchantRepository; + + `@Mock` + private BillingServiceGrpcClient billingServiceGrpcClient; + + `@Mock` + private KafkaProducer kafkaProducer; `@InjectMocks` private MerchantService merchantService;🤖 Prompt for AI Agents
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.
@coderabbitai -- Good one. Follow-up: If that is the case, I don't see any issue until now as both test cases seem to pass, why? Are you proposing this to be an issue now or with the test cases that need to be added in the future?
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.
🧩 Analysis chain
🏁 Script executed:
Length of output: 3887
@Rd4dev— Great follow-up question. Here's exactly what's happening:Why the current tests pass despite the missing mocks:
Mockito's
@InjectMocksattempts constructor injection first. When it finds the 3-argument constructor but only has one matching mock (MerchantRepository), it passesnullfor the two unmatched parameters (BillingServiceGrpcClientandKafkaProducer). The service instance is created successfully — it just hasnullfor those two fields. SincedeleteMerchant()never calls either of those two fields, the tests don't encounter aNullPointerExceptionand the mocks aren't needed right now.So my original comment is more of a forward-looking concern: the moment you add tests for
createMerchant()or any other method that callsbillingServiceGrpcClientorkafkaProducer, those tests will fail withNullPointerExceptionat runtime unless you add the missing@Mockdeclarations.However, there's a more critical bug in
deleteMerchantitself:The condition is inverted —
id != nullmeans any valid (non-null) UUID will immediately throwRuntimeException. It should beid == null. This means both your current tests, which pass non-null UUIDs, should actually be failing — unless you haven't run the test suite yet. Can you confirm whether you've run the tests locally?Summary:
@MockforBillingServiceGrpcClient/KafkaProducercreateMerchant,updateMerchantif(id != null)→ should beif(id == null)The
id != nullinversion is the more urgent fix.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.
Got it, I just wanted to keep things contained to what the PR needs, will add them as the needs arise, thanks.
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.
@Rd4dev— That's a perfectly reasonable approach — no need to add mocks you won't exercise yet.Just a reminder that the
id != nullinversion indeleteMerchantis a separate, current concern (not a future one). That condition causes valid UUIDs to immediately throwRuntimeExceptionin production. It's worth fixing in this PR before merging:Would you like me to open a follow-up issue for tracking the missing mocks, or are you comfortable handling that informally when the time comes?