MockMvc - модель пуста - PullRequest
       51

MockMvc - модель пуста

0 голосов
/ 27 октября 2019

Я тестирую контроллер, используя JUnit и MocMvc, как показано ниже: тест не пройден, потому что модель пуста, но когда я отлаживаю, я уверен, что атрибуты модели содержат правильное значение Controller:

@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="admin@bbc.com",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

любая помощь приветствуется

1 Ответ

0 голосов
/ 29 октября 2019

@ Комментарий Дейна направляет меня в процессе обучения.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

...