Results 1 to 2 of 2

Thread: Unit test of rest mvc

  1. #1
    Join Date
    Jul 2012
    Location
    Stockholm
    Posts
    5

    Smile Unit test of rest mvc

    Hi

    A question about how to use test-mvc for unit testing.

    I have a simple controller..

    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);
    	 
    		}
    My idea is to keep the controllers so thin as possible.

    To test this controller I am trying to do something like this.

    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(?????????????????????????????);
    		
    	}
    My intention is to check that proper json code is returned,,,,,,

    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

  2. #2
    Join Date
    May 2011
    Posts
    4

    Default

    Based on the Spring MVC Test github, I would try something like:

    Code:
    MockMvcBuilders.annotationConfigSetup(TestConfiguration.class).build()
        .perform(get("/resources/Spring.js"))
            .andExpect(content().type("application/octet-stream"))
            .andExpect(content().string(containsString("Spring={};")));
    Besides Spring MVC Test there is a other possibility, you could use Arquillian Spring Framework Extension in order to test your REST services. Arquillian is a integration test framework that executes your tests in real servlet container like Glassfish or JBoss AS. So you could be able to run your tests against executing application. Farther information could be found here https://github.com/arquillian/arquil...tension-spring and here https://community.jboss.org/message/751307#751307

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •