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() {
}
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.