如何在Spring Boot中使用自定义测试拦截器测试控制器?

时间:2019-06-26 15:20:16

标签: spring-boot dependency-injection interceptor

首先,我的语法太糟糕了,因为我不会英语。

我尝试测试控制器,但发生错误。

NoSuchBeanDefinitionException: No qualifying bean of type 'com.ccstudy.qna.interceptor.LoginSessionCheckInterceptor' available: expected at least 1 bean which qualifies as autowire candidate.

我做了拦截器,拦截器的目的是将路径变量与会话ID进行比较。

我做了1个界面和2个类

public interface LoginSessionCheckInterceptor extends HandlerInterceptor {
}
@Profile("prod")
@Component
@Slf4j
public class LoginSessionCheckInterceptorImpl implements LoginSessionCheckInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AccountSessionDto accountSessionDto = (AccountSessionDto) request.getSession()
                .getAttribute(AccountSessionDto.ATTRIBUTE_NAME);
        Long pathId = getPathId(request);
        validateId(accountSessionDto, pathId);
        return true;
    }

    private Long getPathId(HttpServletRequest request) {
        Map<?, ?> pathVariables = (Map<?, ?>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        return Long.parseLong((String) pathVariables.get("id"));
    }

    private void validateId(AccountSessionDto sessionDto, Long pathId) {
        if (!sessionDto.getId().equals(pathId)) {
            throw new SessionMismatchException();
        }
    }
}
@Profile("dev")
@Component
@Slf4j
public class LoginSessionCheckTestInterceptor implements LoginSessionCheckInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

}

这是我的WebMvcConfigurer.class

@Configuration
@RequiredArgsConstructor
public class MvcConfig implements WebMvcConfigurer {

    private final AccountHandlerMethodArgumentResolver accountHandlerMethodArgumentResolver;
    private final LoginSessionCheckInterceptor loginSessionCheckInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        registry.addViewController("/question/form").setViewName("/pages/form");
        registry.addViewController("/register").setViewName("/pages/register");
        registry.addViewController("/login").setViewName("/pages/login");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginSessionCheckInterceptor)
                .addPathPatterns("/users/{id}")
                .addPathPatterns("/questions/edit/**")
                .addPathPatterns("/questions/delete/**")
                .excludePathPatterns("/users/login");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(accountHandlerMethodArgumentResolver);
    }
}

这是我的AccountControllerTest

@WebMvcTest(AccountController.class)
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
public class AccountControllerTest {

    @Autowired
    private MockMvc mvc;

...

}

我只想使用Component来运行测试代码进行测试。 在这种情况下,LoginSessionCheckTestInterceptor

0 个答案:

没有答案