嗨我正在编写一个程序的单元测试用例。在那个程序中,我正在测试一个方法,它们是这个方法:Collections.sort(Arraylist对象)。 它是这样的。如何避免在测试用例中执行Collection.sort()方法?
public void abc(){
try{
Arraylist<object_class> object=some_method.get(list);
Collections.sort(object);
System.out.print("The whole functon is executing successfully") }
catch(exception e) {
system.out.print("error message") } }
的some_method.get(名单)`方法我打电话发出了一个空的列表,以便当Collections.sort()被调用它转到catch块,不执行剩下的代码。 我该怎么做才能让这个Collections.sort()方法在测试用例运行时不被调用。
PS-上面的代码是唯一的解释的问题和我不能更改主类
在测试类我试图用这种方法
Mockito.doNothing()。当(collections.sort (Mockito.anyList())); //这是不工作
所以我试过这个Mockito.doNothing()。when(collections.class).sort(Mockito.anyList()); //这也不起作用
我可以返回一个模拟列表给对象,但我想知道是否可以阻止Collections.sort()执行。
请帮
提示:请转到帮助中心,阅读如何正确格式化问题中的源代码。例如,你不希望所有那些'>'! – GhostCat