使用Activities和Places的GWT MVP测试用例,以及使用Mockito的GIN测试用例



我是GWT MVP模式的新手,该模式具有带有GIN组合的Activities和Places。我已经开始尝试使用Mockito为我的GWT项目编写一个JUnit测试用例。很多博客建议我不要使用GWT测试用例,因为它的性能,所以我计划坚持使用Mockito。我正在为其中一位演示者编写一个测试用例。由于我使用GIN为Presenter中的大多数内容创建实例,所以我必须模拟我的GinInjector对象。我的Junit测试用例不允许我模拟杜松子酒注射器。我在某个地方读到,我们不能在Junit测试用例中使用Gin,而是必须使用Guice。我的问题是,我如何使用Mockito模拟我的杜松子酒注射器?我发现一些测试用例使用了与我的项目完全相同的模式,只是他们使用了客户端工厂。在测试用例中,我未能用GIN替换客户端工厂。我在网上找到的代码如下,我必须在测试用例中将ClientFactory替换为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());
}

}

请帮忙。

如果您的代码依赖于Ginjector,那么您就有一个问题:您没有注入直接依赖项。如果您需要一个对象工厂,请注入一个Provider

但在您的情况下,IClientFactory还充当共享状态值持有者List<Contact>本地缓存;这意味着CCD_ 5违反了单一责任原则。

因此,首先将本地缓存责任提取到其自己的对象(例如ContactListCache对象或更通用的ValueHolder<List<Contact>>)中,然后注入该对象的实例
当然,直接注入PlaceController、视图和GWT-RPC服务。

但实际上,我会更进一步,重构代码以从缓存中提取检索,或者要求服务器将责任放入其自己的ContactListHolder对象中(或者,当您使用GWT-RPC时,您可以将IContactServiceAsync接口实现为GWT.create()生成的接口的包装器,并添加缓存行为;请参阅http://www.google.com/events/io/2009/sessions/GoogleWebToolkitBestPractices.html以获得一些灵感)。这将大大简化活动的代码。


顺便说一句,这段代码可能过于使用了嘲讽:为什么不使用真正的PlaceController(以及spy()goTo(Place)方法)和SimpleEventBusCountingEventBus呢?

我所做的是使用GwtLockito,它是Mockito的扩展。在我使用注入器的唯一一个类中,我的测试看起来像这个

@RunWith(GwtMockitoTestRunner.class)
public class AppControllerTest {
    AppController controller;
    @Mock
    EventBus eventBus;
    @Mock
    AppInjector injector;
    @Before
    public void setUp() throws Exception {
    }
    @Test
    public void shouldUseInjector() throws Exception {
        // Given
        controller = new AppController(eventBus, injector);
        // When
        controller.go();
        // Verify
        verify(injector).mainPresenter();
    }
}

相关内容

  • 没有找到相关文章

最新更新