How to get EasyMock mock to return an empty list multiple times

I would like the EasyMock layout to wait for an empty list several times, even if items are added to the list that returns for the first time.

Is it possible? Since an empty list created on hold is retained for the entire play and therefore saves any items added to it between calls.

Here is a sample code showing what I'm trying to avoid:

public class FakeTest { private interface Blah { public List<String> getStuff(); }; @Test public void theTest(){ Blah blah = EasyMock.createMock(Blah.class); //Whenever you call getStuff() an empty list should be returned EasyMock.expect(blah.getStuff()).andReturn(new ArrayList<String>()).anyTimes(); EasyMock.replay(blah); //should be an empty list List<String> returnedList = blah.getStuff(); System.out.println(returnedList); //add something to the list returnedList.add("SomeString"); System.out.println(returnedList); //reinitialise the list with what we hope is an empty list returnedList = blah.getStuff(); //it still contains the added element System.out.println(returnedList); EasyMock.verify(blah); } } 
+4
source share
1 answer

You can use andStubReturn to generate a new list every time.

 //Whenever you call getStuff() an empty list should be returned EasyMock.expect(blah.getStuff()).andStubAnswer(new IAnswer<List<String>>() { @Override public List<Object> answer() throws Throwable { return new ArrayList<String>(); } } 
+8
source

Source: https://habr.com/ru/post/1338461/


All Articles