简体   繁体   English

EasyMock:如何模拟对象映射

[英]EasyMock: How to mock map of Objects

I am new to EasyMock, tried searching solution to my problem but cudn't find. 我是EasyMock的新手,曾尝试搜索解决我的问题的方法,但找不到。 I have a class "Student" which has several attributes one being "student_id", it has all setter and getter methods . 我有一个“学生”类,它具有几个属性,其中一个是“ student_id”,它具有所有的setter和getter方法。 I have written a function like this which takes Map of Student objects as key and value is their CGPA, it returns a Map having top 5 students by CGPA. 我编写了这样的函数,该函数将Student Map对象作为键,值是其CGPA,它返回一个CGPA排名前5位的Map。

Map<Student,Double>  getMaxCPAStudent(Map<Student,Double>)  

In my test class I write: 在我的测试课中,我写道:

control = EasyMock.createControl();
mockStudent = control.createMock(Student.class);  

But while putting them in Map with different scores they will all have the same key, how to test this function ?? 但是,当将它们以不同的分数放入Map中时,它们都将具有相同的键,如何测试此功能?

Thanks, Amit 谢谢阿米特

I think that EasyMock overrides hashcode() and equals() in its mocks so creating multiple mock Students will never have the same hash. 我认为EasyMock在其模拟中覆盖hashcode()equals() ,因此创建多个模拟学生永远不会拥有相同的哈希。 This means you should be able to put them all in a Map just fine. 这意味着您应该可以将它们全部放入地图中。

So your test code shouldn't be any different than if you were writing production code with this method. 因此,与使用这种方法编写生产代码相比,您的测试代码应该没有什么不同。 Create some mock students, put them in a Map and then call your max method to get a new map back and check that the keys and values are correct. 创建一些模拟学生,将其放入Map中,然后调用max方法以获取新的Map,并检查键和值是否正确。

slightly off topic, unless you are using an Ancient version of Easymock, you should never have to deal with MockControl class. 稍微偏离主题,除非您使用的是Easymock的古代版本,否则您永远不必处理MockControl类。

Student mockStudent = EasyMock.createMock(Student.class);

Should work just fine. 应该工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM