MockMvc模型为空

问题描述 投票:0回答:1

我正在使用下面的JUnit和MocMvc测试控制器:测试失败,因为模型为空,但是在调试时,我确定模型属性包含正确的值控制器:

@RequestMapping("/login/role")
public String roleSelection(Model model, HttpServletRequest request, HttpSession session) {
String name= request.getParameter("Name");        
model.addAttribute("organization", name);
// some code to generate url 
return  "redirect:"+url;  
} 

JUnit:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@ContextConfiguration  
@WebAppConfiguration
public class YXControllerTest {

 @Autowired
 private WebApplicationContext context;

 @Autowired
 private MockHttpServletRequest request;

 @Autowired
 private MockHttpSession session;

private MockMvc mvc;
@Before
public void setUp() throws Exception {
     MockitoAnnotations.initMocks(this);
     mvc = MockMvcBuilders.webAppContextSetup(context)
              .apply(springSecurity())
              .build()  }
 @Test
@WithMockUser(value="[email protected]",password = "pasword")

public void givenAuthRequestOnPrivateService() throws Exception {

     List<String> authorityList = new ArrayList<String>();
     authorityList.add("USER");
     Map<String, List<String>> roleMap = new HashMap();
     roleMap.put("ROL", authorityList);

     this. mvc.perform(get("/login/role").param("Name", "ROL") ).andDo(print())
    .andExpect(status().is3xxRedirection())
    .andExpect(view().name("redirect:/itera/clients"))
    .andExpect( model().attribute("organization", hasSize(1)));
   ...
   }

Print()显示;

  ModelAndView:
    View name = redirect:/itera/clients
         View = null
        Model = null

欢迎任何帮助

spring spring-mvc junit mockmvc
1个回答
0
投票

@ Deinum评论将指导我的学习过程。

© www.soinside.com 2019 - 2024. All rights reserved.