Hi
A question about how to use test-mvc for unit testing.
I have a simple controller..
My idea is to keep the controllers so thin as possible.Code:@Controller @RequestMapping("/users") public class UserController { private UserService business; @Autowired public UserController(UserService bus) { business = bus; } @RequestMapping(value="{id}", method = RequestMethod.GET) public @ResponseBody User getUserById(@PathVariable String id) throws ItemNotFoundException{ return business.GetUserById(id); }
To test this controller I am trying to do something like this.
My intention is to check that proper json code is returned,,,,,,Code:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:mvc-dispatcher-servlet.xml"}) public class UserControllerTest extends ControllerTestBase { UserService mockedService; @Before public void Setup() { MockitoAnnotations.initMocks( this ); mockedService = mock(UserService.class); } @Test public void ReturnUserById() throws Exception{ User user = new User(); user.setName("Lasse"); stub(mockedService.GetUserById("lasse")).toReturn(user); MockMvcBuilders.standaloneSetup(new UserController(mockedService)).build() .perform(get("/users/lasse")) .andExpect(status().isOk()) .andExpect(?????????????????????????????); }
I am not a pro,,, so I have not found a way to replace ??????????????????????? to do verify the returned string but I am certain that there must be a elegant way to do this
Can anyone fill me in?
//lg



Reply With Quote
