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   luzhouxiaoshuai  阅读(264)  评论(0编辑  收藏  举报

编辑推荐:
· 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保存用户偏好设置参数

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示