考虑到下面的代码是最新版本的SpringBoot应用程序的一部分,在一些项目中,测试结果是无法启动应用程序,因为它未能将ErrorMessages注入ControllerAdvice。但由于某些原因,在其他项目中,此代码通过。我在配置、gradle-build或注解方面没有任何差异,所以不完全确定我是否发现了bug。是否有任何方法可以影响在@WebMvcTest上读取和注入的ErrorMessages?
控制器上的控制器文件夹
@RestController
@Api(value = "Some Service", tags = "Some Service")
public class SomeController {
@Autowired
SomeService someService;
@ApiOperation(value = "Gets something")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 500, message = "Internal server error", response = ErrorResponse.class) })
@GetMapping(value = "/something", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SomeResponse> getSomething(
@AuthenticationPrincipal CustomOAuth2Authentication authentication,
) throws ApiException {
return ResponseEntity.ok(someService.get());
}
}
异常文件夹上的控制器建议
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class GlobalExceptionHandler {
@Autowired
private ErrorMessages errorMessages;
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Throwable.class)
@ResponseBody
public ErrorResponse handler(HttpServletRequest request, final Throwable ex)
{
log.error("Unexpected error", ex);
return new ErrorResponseBuilder().with(it -> {
it.status = ErrorCodes.INTERNAL_SERVER_EXCEPTION;
it.error = ex.getLocalizedMessage();
it.exception = ex.getClass().getName();
it.messages = Collections.singletonList(errorMessages.internalServerException);
it.path = request.getRequestURI();
}).build();
}
}
errorMessages on Exception文件夹(从Spring Cloud读取)
@Component
public class ErrorMessages {
@Value("${"+ErrorCodes.INTERNAL_SERVER_EXCEPTION +"}")
public String internalServerException;
}
异常文件夹上的错误代码
public class ErrorCodes {
private ErrorCodes() {}
public static final String INTERNAL_SERVER_EXCEPTION = "3031";
}
测试文件夹上的TestClass
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = SomeController.class)
@ActiveProfiles("test")
@ContextConfiguration(classes = {ErrorMessages.class,
GlobalExceptionHandler.class})
public class SomeControllerTest {
private String SERVICE_URL = "/some";
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() throws ApiException {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(this.wac)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}
@Autowired
ErrorMessages errorMessages;
@MockBean
private SomeService someService;
@Autowired
@InjectMocks
private SomeController someController;
@Test
@WithMockCustomUser()
public void getCheckout_Exception() throws Exception {
ApiException e = new ApiException("code123", "message123");
when(someService.get(anyString(), anyInt())).thenThrow(e);
mockMvc.perform(get(SERVICE_URL))
.andExpect( status().is( equalTo(
HttpStatus.INTERNAL_SERVER_ERROR.value())))
.andExpect( content().string(containsString(e.getClass().getName())))
.andExpect( content().string(containsString(e.getCode())))
.andExpect( content().string(containsString(e.getMessage())))
.andExpect( content().string(containsString(errorMessages.transformingException)));
}
}
错误是
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:99)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'globalExceptionHandler': Unsatisfied dependency expressed through field 'errorMessages'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.comp.proj.service.something.exception.ErrorMessages' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
2条答案
按热度按时间jdgnovmf1#
您可以尝试在测试类中导入异常处理程序:
e5njpo682#
错误的根本原因是您正在使用@WebMvcTest。此注解仅加载使用@Controller,@RestController,@ControllerAdvice注解的bean。它跳过所有其他带注解的SpringBean(例如@component、@service和其他)
我猜你的bean“ErrorMessage”是用这些被排除的注解之一注解的,你会得到下面的错误:通过字段“errorMessages”表示的不满足的依赖关系
解决方案:您可以使用@SpringBootTest而不是@WebMvcTest。唯一的缺点是springboottest将加载所有的springbean,因此速度较慢。
如果你有任何不使用“ErrorMessage”依赖项的选项,那么这个错误本身就会消失。注意:ErrorMessage似乎是数据,无论如何都不应该自动连接,因为spring bean默认情况下是单例的,因此可能会导致其他功能问题。