spring assertThathrownBy未捕获异常

puruo6ea  于 2022-10-30  发布在  Spring
关注(0)|答案(1)|浏览(543)

我正在尝试测试underTest.createBookingGroup(...),如果一些传入参数错误,会导致IllegalStateException。下面是我的单元测试代码:

@Test
    void createBookingGroupWithOneTakenPlace() {
        BookingRequest booking = new BookingRequest();
        booking.setEnteringDate(LocalDateTime.of(2022, 7, 2, 12,0));
        booking.setLeavingDate(LocalDateTime.of(2022, 7, 10, 12,0));
        booking.setSourceFunding(SourceFunding.PUBLIC_INSURANCE);
        booking.setTypeOfBooking(TypeOfBooking.INDIVIDUAL);

        Set<Long> places = Stream.of(place1, place2, place3).map(Place::getId).collect(Collectors.toSet());

        given(placeRepository.findById(place1.getId())).willReturn(Optional.of(place1));
        given(placeRepository.findById(place2.getId())).willReturn(Optional.of(place2));
        given(placeRepository.findById(place3.getId())).willReturn(Optional.of(place3));
        given(userRepository.findById(appUser.getId())).willReturn(Optional.of(appUser));
        given(companyRepository.findById(company.getId())).willReturn(Optional.of(company));
        given(bookingRepository.findByDateAndPlaceId(booking.getEnteringDate(),
                booking.getLeavingDate(),
                place1.getId())).willReturn(List.of(currentBooking));

        Booking booking1 = new Booking();
        booking1.setId(1000L);
        when(bookingRepository.save(any(Booking.class))).thenReturn(booking1);

        assertThatThrownBy(
                 // !! java.lang.IllegalStateException happens here 10000% !!
                () -> underTest.createBookingGroup(booking,
                appUser.getId(),
                company.getId(),
                null,
                places)
        ).isInstanceOf(IllegalStateException.class)
                .hasMessageContaining("The booking with entering date");

    }

带这些参数的underTest.createBookingGroup抛出了10000%的异常,我测试了很多次。但是由于某种原因,我得到了测试失败。看起来像是在代码之前抛出了异常,这很奇怪。

java.lang.IllegalStateException: The booking with entering date 2022-07-02T12:00 and leaving date 2022-07-10T12:00 already taken by at least one booking with id:1
    at pro.dralex.Leo.booking.BookingService.checkAvailableDates(BookingService.java:81)
    at pro.dralex.Leo.booking.BookingService.createBooking(BookingService.java:106)
    at pro.dralex.Leo.booking.BookingService.createBookingGroup(BookingService.java:223)
    at pro.dralex.Leo.booking.BookingServiceTest.lambda$createBookingGroupWithOneTakenPlace$20(BookingServiceTest.java:623)
    at org.assertj.core.api.ThrowableAssert.catchThrowable(ThrowableAssert.java:63)
    at org.assertj.core.api.AssertionsForClassTypes.catchThrowable(AssertionsForClassTypes.java:878)
    at org.assertj.core.api.Assertions.catchThrowable(Assertions.java:1337)
    at org.assertj.core.api.Assertions.assertThatThrownBy(Assertions.java:1181)
    at pro.dralex.Leo.booking.BookingServiceTest.createBookingGroupWithOneTakenPlace(BookingServiceTest.java:622)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at 
...

java.lang.AssertionError: 
Expecting code to raise a throwable.

    at pro.dralex.Leo.booking.BookingServiceTest.createBookingGroupWithOneTakenPlace(BookingServiceTest.java:622)
...
vwkv1x7d

vwkv1x7d1#

对于assertThatThrownBy方法检测到的异常,它必须在执行lambda时抛出,* 并且 * 异常必须到达Assert方法。换句话说:如果你的代码已经捕获并吞下了异常,那么它将永远不会到达Assert。2Assert只能检测未捕获的异常,而不能检测任何抛出的异常。
确保异常在执行lambda时确实被抛出,并且在它冒泡之前没有在其他地方被捕获,并且它可以到达Assert方法。
例如,你的代码可以捕获异常并打印其堆栈跟踪,这就解释了为什么你“看到了异常”,但实际上它并没有到达你的Assert。

void createBookingGroup(…) {
  try {
    // more code …
  } catch (final Exception ex) 
    ex.printStackTrace();
  }
}

相关问题