Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Framework TEST RESTful Web Service (Controller) Offline i.e. No Server, No Database
    text
    copied!<p>I have a very simple RESTful Controller that consumes and produces JSON. I need to test this controller offline i.e. no server running, no database running. And I am going nuts for not being able to find a solution. My intial test cases will include:</p> <ul> <li>Test REST URIs i.e. GET, POST, PUT, DELETE - I must be able to Assert data returned against data sent.</li> <li>Assert will test JSON data</li> </ul> <p>I have the following URIs:</p> <ul> <li>/pcusers - Returns all users</li> <li>/pcusers/{id} - Return a specific user</li> <li>/pcusers/create/{pcuser} - Add user to db</li> <li>/pcusers/update/{pcuser} - Update user</li> <li>/pcusers/delete/{id} - Delete User</li> </ul> <p>NOTE: This is NOT a typical MVC application. I DO NOT have Views. I have a pure REST controller that spits out JSON and consumes data in JSON format.</p> <p>If someone could guide me in the right direction would be really appreciated.</p> <p>Just to be clear how my code looks like:</p> <pre><code>@Controller @RequestMapping("/pcusers") public class PcUserController { protected static Logger logger = Logger.getLogger(PcUserController.class); @Resource(name = "pcUserService") private PcUserService pcUserService; @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") @ResponseBody public List&lt;PcUser&gt; readAll() { logger.debug("Delegating to service to return all PcUsers"); return pcUserService.readAll(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET, consumes = "application/json", produces = "application/json") @ResponseBody public PcUser read(@PathVariable String id) { logger.debug("Delegating to service to return PcUser " + id); return pcUserService.read(id); } @RequestMapping(value = "/create/{pcUser}", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") @ResponseBody public boolean create(@PathVariable PcUser pcUser) { logger.debug("Delegating to service to create new PcUser"); return pcUserService.create(pcUser); } @RequestMapping(value = "/update/{pcUser}", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") @ResponseBody public boolean update(@PathVariable PcUser pcUser) { logger.debug("Delegating to service to update existing PcUser"); return pcUserService.update(pcUser); } @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") @ResponseBody public boolean delete(@PathVariable String id) { logger.debug("Delegating to service to delete existing PcUser"); return pcUserService.delete(id); } } </code></pre> <p><strong>UPDATE (2/5/2012):</strong> After some research, I came across a Spring framework called <strong>spring-test-mvc</strong>. It looks very promising and I have managed to get a good start on this. But now I have a new problem. When I submit a GET request to "/pcusers/{id}", the control is passed to <strong>read</strong> method which is responsible for handling that mapping. Inside that method I have a <strong>pcUserService</strong> that does a read. Now, the problem is when I run this test, the <strong>pcUserService</strong> instance inside real controller is NULL; and therefore it ends up crashing as read cannot be called on a NULL object.</p> <p>Here's <strong>PcUserControllerTest</strong> code:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/applicationContextTest.xml") public class PcUserControllerTest { @Autowired PcUserService pcUserService; @Autowired PcUserController pcUserController; PcUser pcUser; @Before public void setUp() throws Exception { pcUser = new PcUser("John", "Li", "Weasley", "john", "john", new DateTime()); pcUserService.create(pcUser); } public void tearDown() throws Exception { pcUserService.delete(pcUser.getId()); } @Test public void shouldGetPcUser() throws Exception { standaloneSetup(pcUserController) .build() .perform(get("/pcusers/" + pcUser.getId()).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload