我有一个非常简单的 RESTful 控制器,它使用和生成 JSON。我需要离线测试这个控制器,即没有服务器运行,没有数据库运行。而且我因为找不到解决方案而发疯。我的初始测试用例将包括:
- 测试 REST URI,即 GET、POST、PUT、DELETE - 我必须能够针对发送的数据断言返回的数据。
- 断言将测试 JSON 数据
我有以下 URI:
- /pcusers - 返回所有用户
- /pcusers/{id} - 返回特定用户
- /pcusers/create/{pcuser} - 将用户添加到数据库
- /pcusers/update/{pcuser} - 更新用户
- /pcusers/delete/{id} - 删除用户
注意:这不是典型的 MVC 应用程序。我没有意见。我有一个纯 REST 控制器,它可以输出 JSON 并使用 JSON 格式的数据。
如果有人能引导我朝着正确的方向前进,我将不胜感激。
只是为了清楚我的代码如何:
@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<PcUser> 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);
}
}
更新(2/5/2012): 经过一番研究,我发现了一个名为spring-test-mvc的 Spring 框架。它看起来很有希望,我已经设法在这方面取得了良好的开端。但现在我有一个新问题。当我向“/pcusers/{id}”提交 GET 请求时,控件被传递给负责处理该映射的读取方法。在该方法中,我有一个pcUserService进行读取。现在,问题是当我运行这个测试时,真实控制器中的pcUserService实例为 NULL;因此它最终崩溃,因为无法在 NULL 对象上调用 read。
这是PcUserControllerTest代码:
@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());
}
}