-1

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));

    }
Mert Özler
  • 113
  • 5

1 Answers1

1

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.

patrinox
  • 76
  • 7
  • I've already tried this, and tested it again considering the warning, but the queryParam always takes a string. First I created it as localDate and then I did my test but in vain. – Mert Özler Oct 28 '22 at 14:45
  • Okay, with a search through this site, I found a solution for your case. This error you're facing is probably occurs because of the mismatching of expected date type and the value you're sending. If you change parameters of your controller to `@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate`, then you'll successfully parse the value given in your test. I'll update my answer to add reference for this. – patrinox Oct 31 '22 at 06:50