Ошибка теста после обновления до Spring Boot 3 и Java 21JAVA

Программисты JAVA общаются здесь
Anonymous
Ошибка теста после обновления до Spring Boot 3 и Java 21

Сообщение Anonymous »

Этот проект раньше был на Spring Boot Version 2.7.5 .
В настоящее время переключается на Spring Boot 3 и пытается заставить этот тест работать.
импорт org.springframework.test.web.servlet.setup.mockmvcbuilders; < /p>
@InjectMocks
private MyController controllerUnderTest;

private MockMvc mockMvc;

@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders
.standaloneSetup(controllerUnderTest)
.setHandlerExceptionResolvers(
ControllerTestHelper.createExceptionResolver()).build();
}

@Test
public void testData() throws Exception {
MyRequest mockRequest = mock(MyRequest.class);
when(myService.getRequest()).thenReturn(mockRequest);

when(myService.getFill(mockRequest)).thenThrow(new AnotherException("Test exception"));

mockMvc.perform(post("/api{market}/{language}", "invalidMarket", "invalidLanguage")
.andExpect(result -> {
Throwable ex = result.getResolvedException();
Throwable cause = ex.getCause();
assertNotNull(cause);
assertInstanceOf(CustomException.class, cause);
});
}

Мы перешли на Spring Boot 3.4.2 и java 21 , получив следующую ошибку для этого теста.

Jakarta.servlet.servletexception: Обработка запроса не удалась:
company.exceptions.customexception: myController.getData Exception
пойман: Invalidmarket /Invalidlanguage < /p>
at
орг.springframework.web.servlet.frameworkservlet.processRequest(frameWorkservlet.java:1022)
at
at jakarta.servlet.http.httpservlet.service (httpservlet.java:590) на
орг.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:8855) p>

Прошел через тест, и он вводит блок Catch и бросает этот customexception , что я хочу для этого теста. < /p>
Это тестируемый метод. < /p>
@PostMapping(value = "/api{market}/{language}")
public @ResponseBody MyResponse getData() throws AnotherException {
// removed lots of logic to keep it minimal
try {
MyResponse customResponse = myService.getFill(myService.getRequest(); // this fails as intended
return customResponse;
} catch (AnotherException ex) {
// I enter here as intended
throw new CustomException(errorMsg, ex);
}
}

В чем проблема? >
Желаю проверки теста, что я сделал, что я сделал Customexception .
Также добавление Controlertesthelper , если он соответствует.
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

public class ControllerTestHelper {

public static ExceptionHandlerExceptionResolver createExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {};
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
}


Подробнее здесь: https://stackoverflow.com/questions/794 ... nd-java-21

Вернуться в «JAVA»