重定向的URL = http:// localhost / login在需要身份验证的控制器方法中

时间:2017-10-18 10:32:06

标签: spring spring-boot mocking

我正在测试需要身份验证的控制器,我已尽最大努力执行了身份验证,并且仍然继续进行重定向, 调试后显示登录有效,所以我不明白为什么它继续显示在下面

Redirected URL = http://localhost/login
          Cookies = []

java.lang.AssertionError: No ModelAndView found

    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:35)

这是春季启动,这是我的测试和控制器方法。

  @RequestMapping("/bookDetail")
    public String bookDetail(
            @PathParam("id") Long id, Model model, Principal principal
    ) {
        if (principal != null) {
            String username = principal.getName();
            User user = userService.findByUsername(username);
            model.addAttribute("user", user);
        }

        Book book = bookService.findOne(id);

        model.addAttribute("book", book);

        List<Integer> qtyList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        model.addAttribute("qtyList", qtyList);
        model.addAttribute("qty", 1);

        return "bookDetail";
    }

并测试。

@Test
    public void checkBookDetail() throws Exception {
        LoginStatus loginStatus = securityService.login("user", "78fa095d-3f4c-48b1-ad50-e24c31d5cf35");

        bookService = createMock(BookService.class);
        ReflectionTestUtils.setField(homeController, "bookService", bookService);
        Book book = new Book();
        book.setId(1L);
        expect(bookService.findOne(  anyLong())).andReturn(book);
        replay(bookService);

        mockMvc
                .perform(get("/bookDetail")
                        .accept(MediaType.TEXT_HTML)
                        .contentType(MediaType.TEXT_HTML)
                        .param("id", "1"))
               .andExpect(model().attributeExists("book"))
                .andExpect(model().attributeExists("qty"))
                .andExpect(model().attributeHasErrors("user"))
                .andExpect(model().attributeExists("qtyList"))
                .andExpect(content().contentType(MediaType.TEXT_HTML))
                .andReturn();
    }

我在这里缺少什么?

0 个答案:

没有答案