1
回調PARAM我在單元測試非常新和不知道如何在回調測試以下circunstances這個例子類:測試用的Mockito
public class Foo {
private final ItemLoader loader;
private Bar bar;
public Foo(ItemLoader loader,Bar bar) {
super();
this.loader = loader;
this.bar=bar;
}
public void getItems(ItemStore.Callback callback) {
List<ItemData> itemData = bar.getItemData();
List<Item> items = this.loader.transform(itemData);
callback.onItemsLoaded(items);
}
}
- 這callback.onItemsLoaded被調用loader.transform結果
我目前的測試是:
public class ExampleTest extends BaseTestCase {
private Foo foo;
@Mock
private Bar mockBar;
@Mock
private ItemLoader mockItemLoader;
@Mock
private ItemStore.Callback itemLoadCallback;
public void setUp() {
MockitoAnnotations.initMocks(this);
foo = new Foo(mockItemLoader, mockBar);
}
public void testGetItems() {
List<ItemData> mockItemData = (List<ItemData>) mock(List.class);
when(mockBar.getItemData()).thenReturn(mockItemData);
foo.getItems(itemLoadCallback);
verify(mockItemLoader).transform(mockItemData);
}
}
它測試:
- 這loader.transform叫
- 這callback.onItemsLoaded叫
但我意識到,如果我改變像Foo.getItems方法的最後一行(公告空):
public void getItems(ItemStore.Callback callback) {
...
callback.onItemsLoaded(null);
}
該測試保持沉着。所以我需要測試callback.onItemsLoaded被稱爲與loader.transform 的結果,所以我修改了測試:
public void testGetItems() {
List<ItemData> mockItemData = (List<ItemData>) mock(List.class);
when(mockBar.getItemData()).thenReturn(mockItemData);
foo.getItems(itemLoadCallback);
verify(mockItemLoader).transform(mockItemData);
List<Item> resultItems = verify(mockItemLoader).transform(mockItemData);
verify(itemLoadCallback).onItemsLoaded(resultItems);
}
但是,在最後一行說Argument(s) are different!
抱怨我怎麼能修復測試