简体   繁体   English

在Mockito中对模拟对象进行排序

[英]Sorting mock objects in mockito

I am testing a class with mockito. 我正在使用模仿测试类。 It contains these methods: 它包含以下方法:

  • add(Sortable s)
  • List<Sortable> get()

The class does several things to the objects internally, and it is dependant on sorting the Sortable objects . 该类在内部对对象做一些事情,它依赖于对Sortable对象的排序

The problem is that I am mocking Sortable, which makes compareTo() always return 0 . 问题是我在模拟Sortable,这使得compareTo()始终返回0 Is there a way to make a Sorting mock that has the original Sortable compareTo() implementation? 有没有一种方法可以使具有原始Sortable compareTo()实现的Sorting模拟? Or are there other ways to solve this? 还是有其他方法可以解决这个问题?

在调用模拟对象的compareTo()方法之前使用如下语句:

when(mockedObject.compareTo(any(Sortable.class))).thenCallRealMethod();

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

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