'Jackson ObjectMapper in JunitTest5 returns source cannot be null

Preamble: I'm learning Java (Spring) with TDD. I have a work-in-progress project here:

Branch: https://github.com/sineverba/online-banking-backend/tree/add-controller

Last commit: https://github.com/sineverba/online-banking-backend/commit/69d643eb81748656200f007bedf213d9352b079a

I'm introducing test for POST call (see add-controller branch to get current state of work).

So, in new controller I added the post call and in the test I wrote:

        var id = 1L;
        var transactionToSave = validBankAccountTransactionsEntity(new BigDecimal(99.99), "First Transaction");
        var savedTransaction = validBankAccountTransactionsEntity(id, new BigDecimal(99.99), "First Transaction");
        
        when(bankAccountTransactionsService.post(transactionToSave))
        .thenReturn(savedTransaction);
        
        mvc.perform(
                post("/api/v1/bank-account-transactions/")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsBytes(transactionToSave))
                )
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.id", is((int) id)))
        .andExpect(jsonPath("$.amount", is(transactionToSave.getAmount())));

I got the error

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: source cannot be null

And the issue is with

[...]
objectMapper.writeValueAsBytes(transactionToSave)
[...]

And in particular the error is

Caused by: java.lang.IllegalArgumentException: source cannot be null
    at org.modelmapper.internal.util.Assert.notNull(Assert.java:53)
    at org.modelmapper.ModelMapper.map(ModelMapper.java:404)
    at com.bitbank.controllers.v1.BankAccountTransactionsController.convertToDto(BankAccountTransactionsController.java:54)

So, at the end, seems error caused by convertToDto method called by post method of controller: return convertToDto(savedBankAccountTransactionsEntity);.

Cannot explain my issue, I don't know how resolve it.

I'm following a guide: https://www.mattianatali.it/come-sviluppare-rest-api-in-tdd-con-spring-boot/

You can see the author wrote same code (of course different example), about at half of page, you can see:

@Test
    void create_shouldSaveTheVehicle() throws Exception {
        var vehicleId = 1L;
        var vehicleToSave = aValidVehicle();

        when(vehicleService.save(vehicleToSave))
                .thenReturn(
                        vehicleToSave
                                .toBuilder()
                                .id(vehicleId)
                                .build()
                );

        mvc.perform(
                post("/api/v1/vehicles")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsBytes(vehicleToSave))
        )
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.id", is((int) vehicleId)))
                .andExpect(jsonPath("$.brand", is(vehicleToSave.getBrand())))
                .andExpect(jsonPath("$.model", is(vehicleToSave.getModel())))
                .andExpect(jsonPath("$.year", is(vehicleToSave.getYear())));
    }

Where is my issue? :(

Thank you.

==================

1 - Add: if I use Postman and I make the call with body

{
    "amount": 100,
    "purpose": "Postman test"
}

it works.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source