简体   繁体   English

如何在控制器测试中设置RedirectAttributes

[英]How set RedirectAttributes in Controller Test

I need test my controller method 我需要测试我的控制器方法

@RequestMapping(path="/add", method = RequestMethod.POST)
public RedirectView addToCart(@ModelAttribute(value="productId") long productId, @ModelAttribute(value="quantity") int quantity, RedirectAttributes redirectAttributes) throws ProductNotFoundException {

  RedirectView redirect = new RedirectView("/product/");
  redirect.setExposeModelAttributes(false);

  try {
    redirectAttributes.addFlashAttribute("flash", shoppingCartService.addQuantity(sCart, productId, quantity));
      } catch (ExceedsProductQuantityException e) {
        e.printStackTrace();
        redirectAttributes.addFlashAttribute("flash", new FlashMessage(e.getMessage(),  FlashMessage.Status.FAILURE));
      }

  return redirect;
}

My test code looks like: 我的测试代码如下:

@Test(expected = ExceedsProductQuantityException.class)
public void addTooManyToCartTest1() throws Exception {
    Product product = productBuilder();
    product.setQuantity(15);

    Purchase purchase = purchaseBuilder(product); // First purchase

    when(productService.findById(1L)).thenReturn(product);
    when(sCart.getPurchase()).thenReturn(purchase);

    mockMvc.perform(MockMvcRequestBuilders.post("/cart/add")
        .param("quantity", String.valueOf(product.getQuantity() + 1))
        .param("productId", "1"))
        .andExpect(MockMvcResultMatchers.model().attribute("flash", "rdValue"))
        .andExpect(MockMvcResultMatchers.flash().attribute("flash", FlashMessage.class));
}

But I get NestedServledException error message, I think its because in my controller method I try to work with RedirectedAttributes, but it's null. 但是我收到NestedServledException错误消息,我认为这是因为在我的控制器方法中,我尝试使用RedirectedAttributes,但它为null。 So, where and how I have to init and set RedirectedAttributes in my test? 因此,在测试中必须在何处以及如何初始化和设置RedirectedAttributes?

Problem wasnt in RedirectAttributes, there was sCart Mock uninitialized. 问题不在RedirectAttributes中,有sCart Mock未初始化。 I believe you dont need give RedirectAttributes with request as other params. 我相信您不需要将RedirectAttributes与其他参数一起提供给请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试该方法是否添加了redirectAttributes?(由MockMvc提供) - How to test that method has added redirectAttributes ?(by MockMvc) 如何使用RedirectAttributes在另一个控制器中调用一个控制器方法 - How to call one controller method in another controller with RedirectAttributes 如何在签名中没有 RedirectAttributes 的方法中访问 RedirectAttributes? - How can I access RedirectAttributes in a method without RedirectAttributes in its signature? Spring Boot-从另一个控制器调用一个控制器而不使用RedirectAttributes - Spring Boot - Calling a controller from another controller without using RedirectAttributes Spring MVC RedirectAttributes thews异常。 怎么修? - Spring mvc RedirectAttributes thews exception. How to fix? 添加RedirectAttributes参数会导致Spring MVC控制器方法中的“模型没有键值”异常 - Adding RedirectAttributes parameter causes “Model has no value for key” exception in Spring MVC controller method 弹簧集成测试和设置字段到控制器 - Spring integration test & set field to controller 如何在 Spring Boot 控制器测试中测试 @ModelAttrbiute? - How to test @ModelAttrbiute in spring boot controller test? 如何更改控制器中的字段进行测试? - How to change a field in controller for test? 如何测试Spring MVC控制器 - How to test a spring mvc controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM