Spring 单元测试

1,引入Spring测试jarspring-test-4.1.6.RELEASE

2,单元测试Service如下:
 
  1. package com.order.service;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import com.jutudi.common.result.GridResult;
  9. import com.jutudi.order.dao.pojo.OrderBuyer;
  10. import com.jutudi.order.service.OrderBuyerService;
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @ContextConfiguration("classpath:spring/applicationContext*.xml")
  13. @Transactional
  14. publicclassOrderBuyerServiceTest{
  15. @Autowired
  16. privateOrderBuyerService service;
  17. @Test
  18. publicvoid testQueryAll()throwsException{
  19. GridResult<OrderBuyer> grid = service.querySellOrders(newOrderBuyer(),1,10);
  20. System.out.println("测试"+ grid.toString());
  21. }
  22. }
3,单元测试Controller
​具体内容,参考官方文档:
 
  1. /**
  2. * 此处必须引入,否则报错status() get() 方法找不到
  3. */
  4. importstatic org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  5. /**
  6. * 此处必须引入,否则报错status() get() 方法找不到
  7. */
  8. importstatic org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import org.junit.runner.RunWith;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.test.context.ContextConfiguration;
  17. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  18. import org.springframework.test.context.web.WebAppConfiguration;
  19. import org.springframework.test.web.servlet.MockMvc;
  20. import org.springframework.test.web.servlet.MvcResult;
  21. import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
  22. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  23. import org.springframework.web.context.WebApplicationContext;
  24. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  25. //@RunWith(SpringJUnit4ClassRunner.class)
  26. //@ContextConfiguration("classpath:spring/applicationContext*.xml")
  27. //@EnableWebMvc
  28. //@WebAppConfiguration
  29. @RunWith(SpringJUnit4ClassRunner.class)
  30. @WebAppConfiguration
  31. @ContextConfiguration("classpath:spring/applicationContext*.xml")
  32. @EnableWebMvc
  33. publicclassAppFundControllerTest{
  34. @Autowired
  35. privateWebApplicationContext wac;
  36. privateMockMvc mockMvc;
  37. privatestaticfinalLogger log =LoggerFactory.getLogger(AppFundControllerTest.class);
  38. @Before
  39. publicvoid setup()throwsException{
  40. this.mockMvc =MockMvcBuilders.webAppContextSetup(this.wac).build();
  41. // this.mockMvc = MockMvcBuilders.standaloneSetup(new AppFundController()).build();
  42. }
  43. @Test
  44. publicvoid testReceive()throwsException{
  45. // 1、mockMvc.perform执行一个请求;
  46. // 2、MockMvcRequestBuilders.get("/user/1")构造一个请求
  47. // 3、ResultActions.andExpect添加执行完成后的断言
  48. // 4、ResultActions.andDo添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
  49. // 5、ResultActions.andReturn表示执行完成后返回相应的结果。
  50. // MvcResult result =
  51. // mockMvc.perform(MockMvcRequestBuilders.get("/jutudiApp/appfindtopfundclass.html"))
  52. // // .andExpect(MockMvcResultMatchers.view().name("user/view"))
  53. // // .andExpect(MockMvcResultMatchers.model().attributeExists("user"))
  54. // // .andDo(MockMvcResultHandlers.print())
  55. // .andReturn();
  56. MvcResult result =this.mockMvc
  57. .perform(get("/jutudiApp/appfindtopfundclass.html").accept(MediaType.ALL).param("id","256"))
  58. .andExpect(status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();
  59. log.debug("查询结果: "+ result.getResponse().getHeader("Content-Type"));
  60. }
  61. }
 
 





posted @ 2015-05-12 21:36  梅尔加德斯  阅读(852)  评论(0编辑  收藏  举报