#How to mock UUID.randomUUID().toString() in JUnit Test

33 messages · Page 1 of 1 (latest)

earnest carbon
rigid cometBOT
#

This post has been reserved for your question.

Hey @earnest carbon! Please use /close or the Close Post button 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.

sage glacier
#

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

amber valley
sage glacier
#

I have an extreme doubt that they cannot

rigid cometBOT
earnest carbon
#

@sage glacier I fixed the issue.

sage glacier
#

@earnest carbon which approach did you use

#

one of those or some staticmocking thing

earnest carbon
sage glacier
#

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();
    }
amber valley
#

oh right another possibility would be to justn create a method doing the UUID stuff

sage glacier
#

still feels bad

amber valley
#

and then just mock that

sage glacier
#

especially since the things you assert are like "this function called twice"