码迷,mamicode.com
首页 > 其他好文 > 详细

EasyMock

时间:2016-02-25 13:54:19      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

逻辑类:

 1 @Service
 2 public class Service {
 3 
 4     @Autowired
 5     private AppRemoteService appRemoteService;
 6 
 7     public String get(String userId){
 8         ReturnData returnData = new ReturnData();
 9 
10         String data = appRemoteService.get(userId);
11         if(null == data || 0 == data.length()){
12             return "error";
13         }
14         try {
15             AppData appData = JsonSerialize.deSerialize(data, AppData.class);
16             returnData.setUserId(appData.getUserId());
17         }catch(JsonSyntaxException ex){
18             return "error";
19         }
20 
21         return JsonSerialize.serialize(returnData);
22     }
23 }

Unit Test:

 1 public class ServiceTest {
 2 
 3     @Test
 4     public void TestService(){
 5 
 6         String userId = "599760";
 7         String originData = ...;
 8         String expectData = ...;
 9 
10         AppRemoteService mock = EasyMock.createMock(AppRemoteService.class);
11         EasyMock.expect(mock.get(userId)).andReturn(originData);
12         EasyMock.replay(mock);
13 
14         Service service = new Service();
15         ReflectionTestUtils.setField(service, "appRemoteService", mock);
16 
17         String realData = service.get(userId);
18         Assert.assertEquals(expectData, realData);
19     }
20 }

使用了Spring框架中的ReflectionTestUtils类将mock实例inject到Service类的属性。

 

EasyMock

标签:

原文地址:http://www.cnblogs.com/yuhangy/p/5216654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!