我是 GWT MVP 模式的新手,它结合了 GIN 的活动和地点。我已经开始尝试使用 Mockito 为我的 GWT 项目编写 JUnit 测试用例。很多博客建议我不要使用 GWT 测试用例,因为它的性能,所以我打算坚持使用 Mockito。我正在为其中一位演示者编写测试用例。由于我使用 GIN 为 Presenter 中的大部分内容创建实例,因此我必须模拟我的 Gin Injector 对象。我的 Junit 测试用例不允许我模拟 Gin 注射器。我在某处读到我们不能在 Junit 测试用例中使用 Gin,而是必须使用 Guice。我的问题是如何使用 Mockito 模拟我的杜松子酒注射器?我发现了一些测试用例,它使用了与我为我的项目所做的完全相同的模式,只是它们使用了客户端工厂。在测试用例中,我未能用 GIN 替换客户端工厂。我在网上找到的代码如下,我必须在我的测试用例中用 GIN 注入器替换客户端工厂。
@RunWith(MockitoJUnitRunner.class) 公共类 ContactListActivityTest {
@Mock
private IClientFactory clientFactoryMock;
@Mock
private PlaceController placeControllerMock;
@Mock
private IContactListView contactListViewMock;
@Mock
private AcceptsOneWidget acceptsOneWidgetMock;
@Mock
private IContactServiceAsync contactServiceAsyncMock;
@Mock
private EventBus eventBusMock;
private List<Contact> contacts;
private Contact contact1;
private Contact contact2;
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
when(clientFactoryMock.getPlaceController()).thenReturn(placeControllerMock);
when(clientFactoryMock.getContactListView()).thenReturn(contactListViewMock);
when(clientFactoryMock.getContactService()).thenReturn(contactServiceAsyncMock);
Answer<Void> answer = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
AsyncCallback<List<Contact>> asyncCallback = (AsyncCallback<List<Contact>>) args[0];
contact1 = new Contact();
contact1.setFirstName("Kai");
contact1.setLastName("Toedter");
contact1.setEmail("kai@toedter.com");
contact2 = new Contact();
contact2.setFirstName("Kai2");
contact2.setLastName("Toedter2");
contact2.setEmail("kai2@toedter.com");
final List<Contact> contacts2 = new ArrayList<Contact>();
contacts2.add(contact1);
contacts2.add(contact2);
asyncCallback.onSuccess(contacts2);
return null;
}
};
doAnswer(answer).when(contactServiceAsyncMock).getAllContacts(any(AsyncCallback.class));
// set the real contacts object, when clientFactory.setContacts is
// called
Answer<Void> setContactsAnswer = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
contacts = (List<Contact>) invocation.getArguments()[0];
// System.out.println("answer() to setContacts(): " + contacts);
return null;
}
};
doAnswer(setContactsAnswer).when(clientFactoryMock).setContacts(any(List.class));
// Return the real contacts object, when clientFactory.getContacts is
// called
Answer<List<Contact>> getContactsAnswer = new Answer<List<Contact>>() {
@Override
public List<Contact> answer(InvocationOnMock invocation) throws Throwable {
return contacts;
}
};
doAnswer(getContactsAnswer).when(clientFactoryMock).getContacts();
}
@Test
public void testGotoPlace() {
ContactListActivity contactListActivity = new ContactListActivity(new ContactPlace(null), clientFactoryMock);
ContactPlace contactPlace = new ContactPlace("kai@toedter.com");
contactListActivity.goTo(contactPlace);
verify(placeControllerMock).goTo(contactPlace);
}
@Test
public void testStartWithEmptyToken() {
clientFactoryMock.setContacts(null); // force RCP
ContactListActivity contactListActivity = new ContactListActivity(new ContactPlace(""), clientFactoryMock);
contactListActivity.start(acceptsOneWidgetMock, eventBusMock);
verify(contactListViewMock).setPresenter(contactListActivity);
verify(contactListViewMock).initialize(contacts);
}
@Test
public void testStartWithToken() {
String token = "kai@toedter.com";
clientFactoryMock.setContacts(null); // force RCP
ContactListActivity contactListActivity = new ContactListActivity(new ContactPlace(token), clientFactoryMock);
contactListActivity.start(acceptsOneWidgetMock, eventBusMock);
verify(contactListViewMock).setPresenter(contactListActivity);
verify(contactListViewMock).initialize(contacts);
verify(contactListViewMock).selectInitialContact(contact1);
verify(eventBusMock).fireEvent(any(ContactViewEvent.class));
}
@Test
public void testMayStop() {
ContactListActivity contactListActivity = new ContactListActivity(new ContactPlace(null), clientFactoryMock);
contactListActivity.start(acceptsOneWidgetMock, eventBusMock);
contactListActivity.mayStop();
verify(contactListViewMock).setPresenter(null);
}
@Test
public void clientFactoryTest() {
List<Contact> testList = new ArrayList<Contact>();
clientFactoryMock.setContacts(testList);
Assert.assertNotNull(clientFactoryMock.getContacts());
}
}
请帮忙。