Home > OS >  Mockmcv query param with localdate MethodArgumentTypeMismatchException exception
Mockmcv query param with localdate MethodArgumentTypeMismatchException exception

Time:11-01

I'm trying to write an integration test with mockmvc, but I'm getting an error. While startDate and endDate, which I receive as request param, normally work without any problems, I get the following error in the test; MethodArgumentTypeMismatchException

My controller

 @GetMapping("/")
    public ResponseEntity<List<TransactionDto>> getTransactionByDateRange(@RequestParam LocalDate startDate,
                                                                          @RequestParam  LocalDate endDate) {

        logger.info("Get transaction request received with date range, start date: {} and end date: {}",
                startDate,
                endDate);

        List<TransactionDto> transactionDtoList = transactionService.findTransactionByDateRange(startDate,endDate);

        if(transactionDtoList.isEmpty()){
            throw new TransactionListIsEmptyException("No transaction data can be found in this date range. "  
                    "Please check the date range you entered.");
        }

        return new ResponseEntity(Response.ok().setPayload(transactionDtoList), HttpStatus.OK);
    }

My Test

@Test
    public void testfindTransactionByDateRange_whenTransactionsAreExists_ShouldReturnTransactionDtoList() throws Exception {
        //given
        LocalDate startDate = LocalDate.of(2020,10,5);
        LocalDate endDate = LocalDate.now();

        Transaction transaction = transactionRepository.save(generateTransaction());
        Transaction transaction2 = transactionRepository.save(generateTransaction());
        Transaction transaction3 = transactionRepository.save(generateTransaction());
        transactionService.createTransaction(transaction);
        transactionService.createTransaction(transaction2);
        transactionService.createTransaction(transaction3);
        List<Transaction> transactionList = new ArrayList<>();
        transactionList.add(transaction);
        transactionList.add(transaction2);
        transactionList.add(transaction3);
        List<TransactionDto> expected = converter.convertList(transactionList);


        //when
        //then
        this.mockMvc.perform(get(TRANSACTION_API_ENDPOINT)
                        .queryParam("startDate","10.10.2015")
                        .queryParam("endDate","10.10.2015"))
                .andExpect(status().is2xxSuccessful())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON));

    }

CodePudding user response:

Edit: After searching a little bit, I found this thread which could be use as a solution for this topic:

How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime"

Since the exact type of LocalDate parameter doesn't match with the one user sending in queryParam, the exceptions is being thrown. Adding @DateTimeFormat to the parameters in the controller with the expected type will solve the issue.


Can you try to give your startDate and endDate parameters with using LocalDate object creation? This is a very good guide that you can follow: ~

https://www.baeldung.com/java-creating-localdate-with-values

The error occurs due to giving string to that LocalDate fields probably.

  • Related