3

I am learning Junit Mockito to test the spring- mvc controller using Spring 3.2 in Intellij. my controller is

 @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String initUserSearchForm(ModelMap modelMap) {
        User user = new User();
        modelMap.addAttribute("User", user);
        return "linkedInUser";
    }

    @RequestMapping(value = "/byName", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    String getUserByName(HttpServletRequest request,@ModelAttribute("userClientObject") UserClient userClient) {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        return getUserByName(userClient, firstName, lastName);
    }

what i have done is i have one form to search the user by name. UserClient Object is a Session Attribute and i tried to write a junit test case for my controller

 @Test
    public void testInitUserSearchForm() throws Exception {
        this.liClient = client.createUserClient();
        mockMvc.perform(get("/user"))
                .andExpect(status().isOk())
                .andExpect(view().name("user"))
                .andExpect(forwardedUrl("/WEB-INF/pages/user.jsp"));
    }

    @Test
    public void testGeUserByName() throws Exception {
        String firstName = "Wills";
        String lastName = "Smith";         
        mockMvc.perform(get("/user-byName"))
                .andExpect(status().isOk());

    }

How do I test my getUserByName method and how would i add session attribute? Please anyone can help me to write testcase with possible tests for that method. Thanks in advance

2
  • What's the purpose of ",@ModelAttribute("userClientObject") UserClient userClient" Commented Jul 22, 2013 at 14:04
  • It is a session attribute when the user login we saved their secret key in session attribute Commented Jul 23, 2013 at 3:40

2 Answers 2

1

hmmm.

You could try

mockMvc.perform(get("/user-byName").sessionAttr("userClientObject", userClientObject))
            .andExpect(status().isOk());

to setup userClientObject in test fixture.

What does "return getUserByName(userClient, firstName, lastName);" exactly do? If it doesn't involve external dependence, just assert your return in andExpect(jsonPath()) clause.

I thought it should be @SessionAttribute by the way.

Sign up to request clarification or add additional context in comments.

11 Comments

It returns the json formated string. How would i test that string is not empty in spring
Try andExpect(content().string("your json string here"))
Is it possible to check if content has string instead of equals
Maybe this helps if you want assert the json string returned. andExpect(jsonPath("fName").exists()//or other convenient method)
It shows error` java.lang.NoClassDefFoundError: com/jayway/jsonpath/InvalidPathException`. Do i need to include any other libraries
|
0

I use

mockMvc.perform(get("/user-byName").flashAttr("userClientObject", userClientObject)) .andExpect(status().isOk())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.