Last active
December 26, 2015 04:08
-
-
Save nbuesing/7090529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.objectpartners.buesing.sample; | |
import com.google.gwt.event.dom.client.ClickEvent; | |
import com.google.gwt.event.dom.client.ClickHandler; | |
import com.google.gwt.event.shared.SimpleEventBus; | |
import com.google.gwt.user.client.Element; | |
import com.google.gwt.user.client.rpc.AsyncCallback; | |
import com.google.gwt.user.client.ui.Button; | |
import com.google.gwt.user.client.ui.HTML; | |
import com.google.gwt.user.client.ui.Label; | |
import com.google.gwtmockito.GwtMock; | |
import com.google.gwtmockito.GwtMockito; | |
import com.google.gwtmockito.GwtMockitoTestRunner; | |
import com.google.gwtmockito.fakes.FakeProvider; | |
import com.objectpartners.buesing.sample.client.SampleApplicationServiceAsync; | |
import com.objectpartners.buesing.sample.client.SampleWidget; | |
import com.sencha.gxt.widget.core.client.Component; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.model.InitializationError; | |
import org.mockito.Mockito; | |
import org.mockito.invocation.InvocationOnMock; | |
import org.mockito.stubbing.Answer; | |
import java.util.Collection; | |
import static org.mockito.Matchers.any; | |
@RunWith(GwtMockitoTestRunner.class) | |
public class SampleWidgetTest { | |
private SampleWidget sampleWidget; | |
private SimpleEventBus simpleEventBus = new SimpleEventBus(); | |
@GwtMock | |
private Button button; | |
@GwtMock | |
private Label label; | |
@GwtMock | |
private Element element; | |
@GwtMock | |
private HTML html; | |
@GwtMock | |
private SampleApplicationServiceAsync sampleApplicationService; | |
private ClickHandler clickHandler; | |
@Before | |
public void setUp() { | |
GwtMockito.useProviderForType(SimpleEventBus.class, new FakeProvider() { | |
@Override | |
public Object getFake(Class aClass) { | |
return simpleEventBus; | |
} | |
}); | |
Mockito.doAnswer(new Answer() { | |
public Object answer(InvocationOnMock invocation) throws Throwable { | |
String msg = (String)invocation.getArguments()[0]; | |
AsyncCallback callback = (AsyncCallback) invocation.getArguments()[1]; | |
callback.onSuccess("Client said: \"" + msg + "\"<br>Server answered: \"Hi!\""); | |
return null; | |
} | |
}).when(sampleApplicationService) | |
.getMessage(Mockito.any(String.class), (AsyncCallback) Mockito.any()); | |
Mockito.when(button.addClickHandler(Mockito.any(ClickHandler.class))).thenAnswer(new Answer() { | |
public Object answer(InvocationOnMock aInvocation) throws Throwable { | |
clickHandler = (ClickHandler) aInvocation.getArguments()[0]; | |
return null; | |
} | |
}); | |
Mockito.when(label.getText()).thenReturn(""); | |
Mockito.when(label.getElement()).thenReturn(element); | |
// Mocking the click handler needs to be done BEFORE the constructor is called, so the 'clickHandler' can be extracted from the SampleWidget. | |
sampleWidget = new SampleWidget(); | |
} | |
@Test | |
public void clickOnButton() { | |
clickHandler.onClick(new ClickEvent(){}); | |
Mockito.verify(element).setInnerHTML("Client said: \"Hello, World!\"<br>Server answered: \"Hi!\""); | |
Mockito.verify(html).setHTML("event fired."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment