当前位置: 首页 > 知识库问答 >
问题:

Mockito和Spring测试MVC的奇怪问题

阳俊德
2023-03-14

我在单元测试下面的Spring MVC控制器方法:

@RequestMapping(value = "/advertisement/family/edit/{advertisementId}", method = RequestMethod.GET, produces = "text/html")
@AdvertisementExistsAndBelongsToMemberCheck
public String editFamilyAdvertisementForm(@ModelAttribute FamilyAdvertisementInfo familyAdvertisementInfo, @PathVariable long advertisementId, Model model, @CurrentMember Member member) {
    FamilyAdvertisement advertisement = advertisementService.findFamilyAdvertisement(advertisementId);
    familyAdvertisementInfo.setFamilyAdvertisement(advertisement);
    populateFamilyAdvertisementModel(model, familyAdvertisementInfo, member);
    return "advertisement/family/edit";
}

上面的自定义注释(@AdvertisementExistsAndBelongsToMemberCheck)基本上是由以下方面建议的:

before(long advertisementId, Member member) : advertisementBelongsToMemberControllerCheck(advertisementId, member) {
        if (!advertisementService.advertisementExistsAndBelongsToMember(advertisementId, member)) {
            throw new AccessDeniedException("Advertisement does not belong to member!");
        }
    }

如果抛出了一个AccessDeniedException,则会启动以下控制器建议异常处理程序:

@ExceptionHandler(AccessDeniedException.class)
@ResponseStatus(value = HttpStatus.FORBIDDEN)
public String accessDeniedException(AccessDeniedException e) {
    return "error/403";
}

现在,以下是我如何尝试测试上述控制器方法:

@ContextConfiguration
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class AdvertisementControllerTest {

    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;

    @Autowired
    private AdvertisementService advertisementService;

    @Before
    public void setup() {
        mockMvc = webAppContextSetup(ctx).build();
        when(advertisementService.advertisementExistsAndBelongsToMember(eq(111), any(Member.class))).thenReturn(Boolean.FALSE);
        when(advertisementService.advertisementExistsAndBelongsToMember(eq(222), any(Member.class))).thenReturn(Boolean.TRUE);
    }

    @Test
    public void shouldAllow() throws Exception {
        mockMvc.perform(get("/advertisement/family/edit/222"))//
                .andDo(print())//
                .andExpect(status().isOk());
    }

    @Configuration
    @EnableSpringConfigured
    static class testConfiguration {
        @Bean
        public AdvertisementController advertisementController() {
            return new AdvertisementController();
        }

        @Bean
        public InternalResourceViewResolver getInternalResourceViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/pages/");
            resolver.setSuffix(".jsp");
            return resolver;
        }

        @Bean
        public AdvertisementService advertisementService() {
            return mock(AdvertisementService.class);
        }

    }
}

测试系统地失败了,下面的堆栈跟踪:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.security.access.AccessDeniedException: Advertisement does not belong to member!
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134)
    at com.bignibou.tests.controller.advertisement.AdvertisementControllerTest.shouldAllow(AdvertisementControllerTest.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.security.access.AccessDeniedException: Advertisement does not belong to member!
    at com.bignibou.aop.AdvertisementExistsAndBelongsToMemberCheckAspect.ajc$before$com_bignibou_aop_AdvertisementExistsAndBelongsToMemberCheckAspect$2$3edd453b(AdvertisementExistsAndBelongsToMemberCheckAspect.aj:34)
    at com.bignibou.controller.advertisement.AdvertisementController.editFamilyAdvertisementForm(AdvertisementController.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    ... 38 more

我不确定我和莫奇托有什么问题...

注意,我确实在路径中指定了/222,这表明我的模拟应该在方面中返回true,并允许控制器方法继续。然而,事实并非如此。

谁能帮忙吗?

共有2个答案

巫坚白
2023-03-14

问题是参数的类型是long,我传递了一个int

改为:

when(advertisementService.advertisementExistsAndBelongsToMember(eq(222L), any(Member.class))).thenReturn(Boolean.TRUE);

以及:

@Test
public void shouldAllow() throws Exception {
    mockMvc.perform(get("/advertisement/family/edit/{advertisementId}", 222L))//
            .andDo(print())//
            .andExpect(status().isOk());
}

解决了这个问题。

水瀚漠
2023-03-14

aspect是否注入了相同的模拟广告服务实例?

 类似资料:
  • 发现了一个非常有趣的问题,在调试后找到了重现它的场景。 因此,如果我有一个具有包范围B的类,它有一些公共方法和扩展它的公共类A: 然后在测试中: 你猜怎么着,我得到了我刚刚抛出的NullPointerException,所以Mockito以某种方式创建了一个“真实”的对象,并调用了一个真正的方法,而不是一个被嘲笑的方法。为什么会这样? 在上面的示例中,我在类<code>B<code>中更改了<co

  • 问题内容: 发现了一个非常有趣的问题,并在调试后找到了重现该问题的方案。 因此,如果我有一个包范围为B的类,该类具有某些公共方法,而公共类A对其进行了扩展: 然后在测试中: 猜猜是什么,我得到了我刚刚抛出的NullPointerException,所以Mockito以某种方式创建了一个“真实”对象并调用了真实方法,而不是模拟了一个方法。为什么这样? 在上面的示例中,我在课堂上进行了更改,以确保不是

  • 出于某些原因,我需要以不同于标准的方式为Spring上下文命名XML文件。 是的,我知道这是一个基本问题,关于堆栈溢出,有几种回答,例如: Spring上下文配置 Spring MVC上下文Hiearchy Spring-MVC:什么是“上下文”和“命名空间”? 然而,我的问题比那些更棘手一点,因为系统只有在web.xml文件同时具有 和 如果我只删除标签A: Java语言io。FileNotFo

  • 我有两个JPA实体,一个带有SDR导出存储库,另一个带有Spring MVC控制器和非导出存储库。 MVC公开实体具有对SDR管理实体的引用。代码参考见下文。 当从用户控制器中检索用户时,问题就出现了。SDR管理的实体不会序列化,而且Spring可能试图在响应中使用HATEOAS引用。 对于一个完全填充的用户,下面是一个如何获取的: 如何清楚地从我的控制器返回带有嵌入式SDR托管实体的实体? Sp

  • 我正在与 Spring框架4.3.3 AspectJ 1.8.9 我有两个,一个用于而另一个用于。每种方法都使用了一个的依赖关系。 我有以下Rest方法 观察我使用的第二个参数 我有以下切入点: 以及以下建议: 通过 当我发送有效数据时: 如: 或者由 通过< code>Gradle报告,我可以确认< code>@Around建议的效果如何。它只是看着AdviceUpdateOne - Class