Junit测试service层和control层

1.测试controller部分代码如下:

[java]  view plain  copy
  1. "font-family:Microsoft YaHei;">@RunWith(SpringJUnit4ClassRunner.class)  
  2. @WebAppConfiguration  
  3. @ContextConfiguration(locations = { "classpath:resources/applicationContext.xml""classpath:resources/springmvc-servlet.xml" })  
  4. public class CommentControllerTest {  
  5.     @Autowired  
  6.     private CommentController commentController;//引入要测试的controller  
  7.       
  8.     private MockMvc mockMvc;  
  9.   
  10.     @Before  
  11.     public void setup() {  
  12.         mockMvc = MockMvcBuilders.standaloneSetup(commentController).build();  
  13.     }  
  14.   
  15.     @Test  
  16.     @Rollback(false//插入需要此注解,不然插不进数据库  
  17.     public void addCommnet(){  
  18.         String result = "" ;  
  19.         try {  
  20.             ResultActions ra = this.mockMvc.perform(MockMvcRequestBuilders.post("/comment/addComment")  
  21.                     .param("startlevel""5").param("commentcontent""环境很好").param("shopid""9562ed5d02b348d1bfec1699ba1df36b")  
  22.                     .param("commentuserid""d07136c725754cd7827a35581d4a4456").param("consumption""20.5")  
  23.                     .param("url""comment/images")  
  24.                     .param("palate""4").param("environment""4").param("service""4")  
  25.                     //.param("key", "[{id:'',imagename:'a1',imageupdatename:'',url:'a1',commentid:'',createdate:''},{id:'',imagename:'a2',imageupdatename:'',url:'a2',commentid:'',createdate:''}]")//前台应出入json格式  
  26.                     .param("imagename""袋鼠,兔子")//前台传入逗号分割的字符串  
  27.                     .param("recommends""蛋炒西红柿,橙汁")  
  28.                     );  
  29.             MvcResult mr = ra.andReturn();  
  30.             result = mr.getResponse().getContentAsString();  
  31.             System.out.println("+++++++++++"+result);  
  32.         } catch (Exception e) {  
  33.             Assert.fail("fail");  
  34.             e.printStackTrace();  
  35.         }  
  36.           
  37.     }  
  38.       
  39. }  
  40.   
2.jUnit测试Service:

[java]  view plain  copy
  1. "font-family:Microsoft YaHei;">@RunWith(SpringJUnit4ClassRunner.class)  
  2. @WebAppConfiguration  
  3. @ContextConfiguration(locations = { "classpath:resources/applicationContext.xml" })  
  4. public class Share_Test {  
  5.       
  6.     @Autowired  
  7.     private ShareService shareService;//分享Service  
  8.       
  9.     /** 
  10.      * 添加分享 
  11.      */  
  12.     @Test  
  13.     public void addShare(){  
  14.         Share share = new Share();  
  15.         share.setId(UUIDUtil.generateKey());  
  16.         share.setUserid(UUIDUtil.generateKey());  
  17.         share.setShopid(UUIDUtil.generateKey());  
  18.         share.setShareaddress("www.baidu.com");  
  19.         try {  
  20.             shareService.insertShare(share);  
  21.         } catch (Exception e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.       
  27.     /** 
  28.      * 删除分享 
  29.      */  
  30.     @Test  
  31.     public void deleteShare(){  
  32.         String id = "109ece2bb310440494150cac78184c93";  
  33.         try {  
  34.             shareService.deleteShareByPrimaryKey(id);  
  35.         } catch (Exception e) {  
  36.             // TODO Auto-generated catch block  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.       
  41.     /** 
  42.      * 查询分享 
  43.      */  
  44.     @Test  
  45.     public void selectShare(){  
  46.         List s = shareService.selectShare();  
  47.         for (Share share : s) {  
  48.             System.out.println("++++++++++++++++++++++++"+share.getShareaddress());  
  49.         }  
  50.     }  
  51.       
  52.       

你可能感兴趣的:(Junit测试service层和control层)