easymock案例2
public interface IStudent { public String doMethod1(); public String doMethod2(); public String doMethod3(); }
public class StudentApplication { IStudent student=null; public StudentApplication(IStudent student) { this.student = student; } public String doMethod(){ String str1=student.doMethod1(); String str2=student.doMethod2(); String str3=student.doMethod3(); return str1+str2+str3; } public IStudent getStudent() { return student; } }
import main.IStudent; import main.StudentApplication; import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Test; public class testStudentApplication { IStudent student; StudentApplication application; @Test public void testdoMethod(){ //•使用 EasyMock 生成 Mock 对象; student=EasyMock.createMock(IStudent.class); //设定 Mock 对象的预期行为和输出 EasyMock.expect(student.doMethod1()).andReturn("a").times(1); EasyMock.expect(student.doMethod2()).andReturn("b").times(1); EasyMock.expect(student.doMethod3()).andReturn("c").times(1); //将 Mock 对象切换到 Replay 状态 EasyMock.replay(student); //调用 Mock 对象方法进行单元测试 application=new StudentApplication(); application.setStudent(student); String getStr=application.doMethod(); //对 Mock 对象的行为进行验证 String cstr="abc";//正确的字符串 Assert.assertEquals(getStr, cstr); EasyMock.verify(student); } }
posted on 2018-04-24 23:22 luzhouxiaoshuai 阅读(264) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2017-04-24 黎活明8天快速掌握android视频教程--22_访问通信录中的联系人和添加联系人
2017-04-24 黎活明8天快速掌握android视频教程--21_监听ContentProvider中数据的变化
2017-04-24 黎活明8天快速掌握android视频教程--20_采用ContentProvider对外共享数据
2017-04-24 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示
2017-04-24 黎活明8天快速掌握android视频教程--18_在SQLite中使用事务
2017-04-24 黎活明8天快速掌握android视频教程--17_创建数据库与完成数据添删改查
2017-04-24 黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数