使用包含自动装配属性的ControllerAdvice进行WebMvc测试

时间:2018-05-15 09:19:06

标签: unit-testing spring-boot java-8 controller autowired

将代码视为具有最新版本的SpringBoot应用程序的一部分,在某些项目中,测试结果是无法启动应用程序,因为它无法将ErrorMessages注入ControllerAdvice。但由于某些原因,其他项目的代码通过了。我在配置,gradle-build或注释方面没有任何差异,所以不完全确定我是否发现了一个bug。 有没有办法影响在@WebMvcTest上读取和注入ErrorMessages?

Controller文件夹上的控制器

@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());
    }
}

Exception文件夹的控制器建议

@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(从Spring Cloud读取)

@Component
public class ErrorMessages {

    @Value("${"+ErrorCodes.INTERNAL_SERVER_EXCEPTION +"}")
    public String internalServerException;

}

异常文件夹上的errorCodes

public class ErrorCodes {
    private ErrorCodes() {}

    public static final String INTERNAL_SERVER_EXCEPTION = "3031";
}

Test文件夹上的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)

1 个答案:

答案 0 :(得分:0)

您可以尝试在测试类中导入异常处理程序:

@RestController
@Api(value = "Some Service", tags = "Some Service")
@Import(GlobalExceptionHandler.class)  // EXCEPTION HANDLER CLASS
public class SomeController {