java.lang.AssertionError:找不到ModelAndView

时间:2018-01-25 22:53:24

标签: java testing mockito

我有一个看起来像这样的测试类

@AutoConfigureMockMvc()
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest({ShoppingCartController.class, HomeController.class})
@ContextConfiguration(classes = {SecurityConfig.class})
public class ShoppingCartControllerTest {

    @Autowired
    WebApplicationContext context;


    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private BookService bookService;

    @MockBean
    private UserService userService;

    @MockBean
    private CartItemService cartItemService;


    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
    }


    @Test
    public void showLoginPage() throws Exception {

        mockMvc.perform(get("/login")
                .accept(MediaType.TEXT_HTML)

                .contentType(MediaType.TEXT_HTML)
        )
                .andExpect(model().attributeExists("classActiveLogin"))
                .andReturn();
    }

    @Test
    @WithMockUser(username = "V", authorities = {"USER"})
    public void addItemToShoppingCart() throws Exception {
        CartItem cartItem = new CartItem();
        String qty = "2";

        Book book = new Book();
        User user = new User();
        book.setId(1L);
        book.getId();

        cartItem.setBook(book);
        when(userService.findByUsername(anyString())).thenReturn(user);


        when(bookService.findOne(anyLong())).thenReturn(book);

        when(cartItemService.addBookToCartItem(book, user, Integer.parseInt(qty))).thenReturn(cartItem);
        ObjectMapper mapper = new ObjectMapper();

        String bookAsString = mapper.writeValueAsString(book);
        mockMvc
                .perform(get("/shoppingCart/addItem")
                        .accept(MediaType.TEXT_HTML)
                        .contentType(MediaType.TEXT_HTML)
                        .param("book", bookAsString)
                        .param("qty", qty))
                .andReturn();
    }



    @Configuration
    @Import({PropertyTestConfiguration.class, SecurityUtility.class})
    static class ContextConfiguration {
    }

}

测试通过了@springBootTest,但是当我使用@WebMvcTest时它没有识别uri。我在下面得到了这个错误。

java.lang.AssertionError: No ModelAndView found

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

这是我的控制器类

@Controller
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private UserService userService;

    @Autowired
    private CartItemService cartItemService;


    @PreAuthorize("hasAuthority('USER')")
    @RequestMapping("/addItem")
    public String addItem(
            @ModelAttribute("book") Book book,
            @ModelAttribute("qty") String qty,
            Model model, Principal principal
    ) {
        User user = userService.findByUsername(principal.getName());


        try {
            book = bookService.findOne(book.getId());

            if (Integer.parseInt(qty) > book.getInStockNumber()) {
                model.addAttribute("notEnoughStock", true);
                return "forward:/bookDetail?id=" + book.getId();
            }

            CartItem cartItem = cartItemService.addBookToCartItem(book, user, Integer.parseInt(qty));
            model.addAttribute("addBookSuccess", true);

        } catch (NullPointerException e) {

        }

        return "forward:/bookDetail?id=" + book.getId();
    }



@Controller
public class HomeController {

    @Autowired
    private JavaMailSender mailSender;

    @RequestMapping("/login")
    public String login(Model model) {
        model.addAttribute("classActiveLogin", true);
        return "Myaccount";
    }

其实mvc.perform得到-uri,不读uri, 任何人都可以告诉我为什么它适用于springbootTest但不适用于WebMvcTest

0 个答案:

没有答案