1. 实例代码
package com.ruoyi.common.utils;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.junit.Assert.*;
class DateUtilsTest {
@Test
void should_throw_run_time_exception_with_wrong_input() {
ExpectedException thrown= ExpectedException.none();
thrown.expect(RuntimeException.class);
DateUtils.dateTime(DateUtils.YYYY_MM_DD, "2020-11-28aaa");
}
}
2. junit5
import static org.junit.jupiter.api.Assertions.*;
...
InspectBasicInfoRepository repository = mock(InspectBasicInfoRepository.class);
when(repository.getInspectIdBySubTaskId(anyLong())).thenReturn(Optional.empty());
QueryBasicService basicService = new QueryBasicServiceImpl(repository);
assertThrows(BusinessValidateException.class, () -> basicService.getInspectId(1L));