#How to mock UUID.randomUUID().toString() in JUnit Test
33 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @earnest carbon! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
A browser interface to the Java compiler to experiment with the language
A browser interface to the Java compiler to experiment with the language
A browser interface to the Java compiler to experiment with the language
Use way #3 if possible
if not, use #1 if you are comfortable using preview features
if not, use #2
the mocking libraries are a powerful poison
these things don't really work whe you can't change the UUID.randomUUID() call...
I have an extreme doubt that they cannot
String approvalCode = UUID.randomUUID().toString();
@sage glacier I fixed the issue.
@earnest carbon which approach did you use
one of those or some staticmocking thing
You can look through my test service class.
wait
im reading this
this ... doesn't actually test any of your code?
unless im reading this wrong
everything is a mock object
yeah what the fuck man
okay okay
@InjectMocks means that the AccountService is "real"
so all of that is just to test these lines indepenently of the database
public AccountDTO create(CreatedAccountRequest request) {
Account account = Account.builder()
.owner(request.getOwner())
.accountNumber(generateUniqueAccountNumber())
.transactions(new HashSet<>())
.build();
Account savedAccount = accountRepository.save(account);
List<TransactionDTO> transactionDTOs = transactionMapper.toTransactionDTOList(
new ArrayList<>(savedAccount.getTransactions())
);
return AccountDTO.builder()
.owner(savedAccount.getOwner())
.accountNumber(savedAccount.getAccountNumber())
.createdDateTime(savedAccount.getCreatedDateTime())
.balance(savedAccount.getBalance())
.transactionDTOs(transactionDTOs)
.build();
}
oh right another possibility would be to justn create a method doing the UUID stuff
still feels bad
and then just mock that
especially since the things you assert are like "this function called twice"