Chirayu Singhvi
Chirayu Singhvi

Reputation: 213

How to avoid Collection.sort() method from executing in a test case?

Hi I am writing unit test cases for a program. In that program I am testing a certain method in which their is this method : Collections.sort(Arraylist object). Its something like this.

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")  } }

the some_method.get(list)` method I am calling is sending a empty list so when Collections.sort() is called it goes to catch block and the rest code is not executed. What can i do so that this Collections.sort() method is not called while test case is running.

PS- above code is only to explain the question and I cant make changes to main class

In test class I tried to use this method

Mockito.doNothing().when(collections.sort(Mockito.anyList()));//this is not working

So I tried this Mockito.doNothing().when(collections.class).sort(Mockito.anyList()); //this is also not working

I can return a mock list to object but I want to know if I can prevent Collections.sort() from executing.

Please help

Upvotes: 1

Views: 1840

Answers (3)

Praveen Kumar Mekala
Praveen Kumar Mekala

Reputation: 638

It' simple if you want to execute Collections.sort() without any exception follow below steps:

1) Create a list object with dummy values in your test class and send it to main class

list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");

2) Second step is make sure that your

some_method.get(list);

returns a ArrayList of objects,

3) As Collections class has static method sort, JVM will execute rest of the code as usual. And will not through any exception.

PS- If Mockito.doNothing() is not working try with PowerMockito.doNothing(), it might work and make sure that you have

1) @PrepareforTest(Collections.class)

2) PowerMockito.mockStatic(Collections.class);

3) PowerMockito.doNothing().when(Collections.sort(obj));

Hope it's useful.

Upvotes: 2

GhostCat
GhostCat

Reputation: 140505

This is wrong on many levels:

  1. You are using a raw type there (by using ArrayList without a generic type parameter) - never do that!
  2. If your test setup makes that other method return null, then don't manipulate "later" code - instead either avoid that null or make sure your production code can deal with it!

In other words: there are only two reasonable choices:

A) if in reality, that method never returns null. Then you should make sure, that this is also true for your test setup.

B) if in reality, that method returns null, too ... then your production code needs to deal with that, for example by doing a null check before calling sort!

Finally: especially for lists, that problem really doesn't exist at all: the answer is - you never ever return null! Instead, you return an empty list if there is no data. Empty lists can be sorted without a problem! Long story short: avoid returning null in the first place.

Upvotes: 1

mm759
mm759

Reputation: 1424

I can return a mock list to object but I want to know if I can prevent Collections.sort() from executing.

I don't see a way to prevent that Collections.sort() is executed as long as the method that calls it is executed and as long as you can't change the tested code. The reason is that sort is a static method. You can pass or inject an instance that is a mock to achieve your goal.

Returning a mock list that can be sorted is the way to go in my opinion. If you manipulate the behavior of the tested class during test you will have a higher risk not to find errors. Additionally, you have a tight coupling between test and tested class in this case.

Upvotes: 0

Related Questions