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

jackyesind
  • 3,343
  • 14
  • 47
  • 74

2 Answers2

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.

Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
  • It returns the json formated string. How would i test that string is not empty in spring – jackyesind Jul 23 '13 at 05:30
  • Try andExpect(content().string("your json string here")) – Yugang Zhou Jul 23 '13 at 06:15
  • Is it possible to check if content has string instead of equals – jackyesind Jul 23 '13 at 12:21
  • Maybe this helps if you want assert the json string returned. andExpect(jsonPath("fName").exists()//or other convenient method) – Yugang Zhou Jul 23 '13 at 15:34
  • It shows error` java.lang.NoClassDefFoundError: com/jayway/jsonpath/InvalidPathException`. Do i need to include any other libraries – jackyesind Jul 24 '13 at 03:59
  • it always null `java.lang.AssertionError: JSON pathfn expected: but was:` i used `.andExpect(jsonPath("fn").value("Sam"))` – jackyesind Jul 24 '13 at 04:28
  • Yes, I find the maven gav com.jayway.jsonpath:json-path:0.5.5 on the internet. – Yugang Zhou Jul 24 '13 at 04:30
  • This http://stackoverflow.com/questions/13362372/testing-with-spring-test-mvc-jsonpath-returns-null maybe helps. – Yugang Zhou Jul 24 '13 at 04:34
  • Is is possible to perform mockito unit test for that above code. I searched in goole but i didn't have idea. Can u help me – jackyesind Jul 26 '13 at 11:41
  • It depends on what's in your getUserByName(userClient, firstName, lastName); is it very complex to setup the test fixture. You might have to post another question to discuss this(due to Stackoverflow rules) – Yugang Zhou Jul 26 '13 at 13:28
  • Hi Hippoom. I posted a new question http://stackoverflow.com/questions/17915828/how-to-write-mockito-test-case. – jackyesind Jul 29 '13 at 03:57
0

I use

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

cicidi
  • 377
  • 3
  • 6