4

初めて返されたリストに要素が追加されている場合でも、EasyMock モックが空のリストを複数回期待できるようにしたいと考えています。

これは可能ですか?期待どおりに作成された空のリストは、リプレイ全体にわたって持続するため、呼び出し間で追加された要素が保持されます。

これは、私が回避しようとしていることを示すコード例です。

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

1 に答える 1

8

andStubReturn を使用して、毎回新しいリストを生成できます。

//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>();
        }
    }
于 2011-02-04T18:42:52.790 に答える