1

How do I test a "resource" in a Spring Controller:

@RequestMapping(value = "/{query}", method = RequestMethod.GET)
public @ResponseBody String getResource(
        HttpServletRequest req, 
        HttpServletResponse res,
        @PathVariable String query,
        @RequestParam(value="param1", required = false) String param1,
        @RequestParam(value="param2", required = false) String param2) throws SomeException{
    return service.read(query);
}

And since I am developing with App Engine I have a JUnit scaffolding like this:

@ContextConfiguration(locations = { "classpath:service/client-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class LocalDatastoreTest {
    private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());  

    @Before
    public void setUp() {
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }   

    @Test
    public void test() {

    }

}
quarks
  • 29,080
  • 65
  • 239
  • 450

1 Answers1

3

If you have a test server up and running, try using Spring RestTemplate in your test method

something like:

RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("YOUR_URL/{query}", String.class,"myQuery");

See more here.

If you don't have a server and need basic unit testing, try mocking one. More info on another stackoverflow question.

Or try using https://github.com/SpringSource/spring-test-mvc

Community
  • 1
  • 1
mfirry
  • 3,474
  • 1
  • 22
  • 33
  • I did use Google Http Client instead of RestTemplate as I can't seem to make it work with App Engine – quarks Aug 08 '12 at 19:25