最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hibernate - Spring 3.4.1 integration test-> Unable to evaluate the children renderer expression Method threw 'jav

programmeradmin5浏览0评论

recently I have updated spring boot from 3.3.8 to newer version which is 3.4.1 and suddenly I have started to face a lot of new problems with integration tests. Few of them were really easy to adjust, but I am struggling with one which is really weird (at least in my opinion). Our integration tests are executed in test containers.

Okay, so here is my example:

My test:

  @Test
  void test1() throws Exception {
    Optional<Customer> priceCalculationCustomer = customerRepository.findById(CustomerId.builder().customerId("PRICE_MAIN").salesOrg("PRIC").build());
    var book = quotationRepository.findByCustomer(priceCalculationCustomer.get()).getFirst();
    BookDto requestBody = BookDto.builder()
                                 .bookId(book.getId())
                                 .build();
    mvc.perform(
         MockMvcRequestBuilders.post(BASE_PATH + BOOK_PATH)
                               .contentType(MediaType.APPLICATION_JSON)
                               .content(convertObjectToJsonBytes(requestBody))
                               .with(jwtRequestPostProcessor))
       .andExpect(status().isOk());
  }

My controller:

  @PostMapping(value = BOOK_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
  public void addQuotationDetails(@RequestBody final BookDto bookDto) {
    addBookDetailService.addBookDto(bookDto);
  }

My service:

  @Transactional
  public void addBookDto(BookDto bookDto) {
    var book = bookFindService.fetchBook(bookDto.getBookId());
    book.getDetails().size();
  }

My book find service:

  public Book fetchBook(long id) {
    return bookRepository.findById(id)
                              .orElseThrow(() -> new NotFoundProblem(
                                "book %s does not exist.".formatted(
                                  id)));
  }

And I have started to face error in my service, when I try to fetch details of book I receive such an error:

Caused by: java.lang.AssertionError: null
Unable to evaluate the expression Method threw 'java.lang.AssertionError' exception.

It looks like entity manager is losing transaction and it is not able to fetch lazy loaded details, but I have no idea why. This problem does not occur in the application, only in our integration test. This is not a single case, I have multiple cases like this (same story).

My book entity:

  @OneToMany(mappedBy = "book", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
  private List<Detail> details;

My book details:

  @ManyToOne(optional = false, fetch = FetchType.LAZY)
  @JoinColumn(name = "id", nullable = false)
  private Book book;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论