gpt4 book ai didi

java - 测试基于注解的 RequestInterceptor

转载 作者:行者123 更新时间:2023-12-01 14:09:15 24 4
gpt4 key购买 nike

我想在某些路线上这样做自定义逻辑(记录请求和响应)。基于一些研究,我决定使用 AnnotationBased RequestInterceptor。这是我的拦截器代码。

public class CustomInterceptor extends HandlerInterceptorAdapter {
@Override
public void afterCompletion(final HttpServletRequest request,
final HttpServletResponse response,
final Object handler,
final Exception ex) {
if (handler != null && handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
CustomRecord annotation = AnnotationUtils.getAnnotation(handlerMethod.getMethod(), CustomRecord.class);
if (annotation != null) {
// Record Request and Response in a File.
}
}

现在这个类按预期工作,但我无法对这个函数进行单元测试。
  • 我首先想到尝试创建一个 HandlerMethod 对象,但我
    没有得到任何地方。
  • 其次,我尝试使用 PowerMokito。这是我的测试代码:
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(CustomInterceptor.class)
    @PowerMockIgnore("javax.management.*")
    public class CustomInterceptorTest {
    @Test
    public void restAnnotationRecording_negetivecase() {
    HandlerMethod mockHandler = mock(HandlerMethod.class);
    PowerMockito.mockStatic(AnnotationUtils.class);
    when(AnnotationUtils.getAnnotation(mockHandler.getMethod(),
    CustomRecord.class).thenReturn(null);
    // Verify file is not saved
    }
    // A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() methodcannot be saved.
    @Test
    public void restAnnotationRecording_happycase() {
    HandlerMethod mockHandler = mock(HandlerMethod.class);
    PowerMockito.mockStatic(AnnotationUtils.class);
    when(AnnotationUtils.getAnnotation(mockHandler.getMethod(), CustomRecord.class).thenReturn(mockAnnotation);
    // Verify file is saved
    }
    }
  • 这给出了一个错误 A spy is stubbed using when(spy.foo()).then() 语法。使用 doReturn|Throw() 系列方法 stub spy 更安全。

  • 我想检查是否有任何方法来测试拦截器。我是Java新手,感谢您的帮助。

    最佳答案

    您可以轻松创建自己的HandlerMethod没有 mock 。有一个constructor接受一个对象( Controller )和一个Method ( Controller 方法)。获得 Method 的最简单方法就是直接调用Class.getMethod() .您要做的只是创建一个虚拟 Controller 类,然后使用该类来获取方法。例如

    class TestController {
    @Custom
    public void testMethod() {}
    }

    Method method = TestController.class.getMethod("testMethod");
    TestController controller = new TestController();
    HandlerMethod handlerMethod = new HandlerMethod(controller, method);

    Custom annotation = handlerMethod.getMethodAnnotation(Custom.class);

    就这么容易。下面是一个完整的测试。
    public class HandlerInterceptorTest {

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    private @interface Custom {
    }

    private static class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) {
    if (handler instanceof HandlerMethod) {
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Custom annotation = handlerMethod.getMethodAnnotation(Custom.class);
    if (annotation != null) {
    return true;
    }
    }
    return false;
    }
    }

    private static class TestController {
    @Custom
    public void testMethodWithAnnotation() {}

    public void testMethodWithoutAnnotation() {}
    }

    @Test
    public void testMethodWithAnnotation() throws Exception {
    Method method = TestController.class.getMethod("testMethodWithAnnotation");
    TestController controller = new TestController();
    HandlerMethod handlerMethod = new HandlerMethod(controller, method);

    MyHandlerInterceptor interceptor = new MyHandlerInterceptor();
    boolean result = interceptor.preHandle(null, null, handlerMethod);

    assertTrue(result);
    }

    @Test
    public void testMethodWithoutAnnotation() throws Exception {
    Method method = TestController.class.getMethod("testMethodWithoutAnnotation");
    TestController controller = new TestController();
    HandlerMethod handlerMethod = new HandlerMethod(controller, method);

    MyHandlerInterceptor interceptor = new MyHandlerInterceptor();
    boolean result = interceptor.preHandle(null, null, handlerMethod);

    assertFalse(result);
    }
    }

    关于java - 测试基于注解的 RequestInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682442/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com