Last active
April 30, 2018 21:27
-
-
Save jeffsheets/02b3d77293c8f8419c0b31b47b627676 to your computer and use it in GitHub Desktop.
GWT Spock test with GwtMockito for Google Web Toolkit client testing
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.sheetsj.myapp.client.widget.fancy | |
import com.google.gwt.event.dom.client.KeyUpEvent | |
import com.google.gwt.user.client.ui.Widget | |
import com.google.gwtmockito.GwtMockito | |
import com.sheetsj.myproj.client.AppController | |
import com.sheetsj.myproj.client.widget.ErrorWidget | |
import org.mockito.Mockito | |
import spock.lang.Specification | |
/** | |
* Shows how to use Spock with GwtMockito to test GWT client code | |
*/ | |
class GwtSpockWidgetSpec extends Specification { | |
AppController appController = Mock() | |
ErrorWidget errorWidget = Mock() | |
def widget | |
void setup() { | |
GwtMockito.initMocks(this) | |
//Requires Spock 1.2 to Spy instantiated object without a no-args constructor | |
// https://github.com/spockframework/spock/issues/769 | |
widget = Spy(new GwtSpockWidget(appController, errorWidget) { | |
void initWidget(Widget w) { /*disarming for test per gwtmockito docs */ } | |
}) | |
} | |
void cleanup() { | |
GwtMockito.tearDown() | |
} | |
void "initialize successfully"() { | |
expect: 'the widget initialized' | |
widget | |
} | |
void "setFancyName function sets fancyName and label"() { | |
given: 'fancyName with a value' | |
widget.fancyName = 'abc123' | |
and: 'a sanity check that the given values were set correctly' | |
assert widget.fancyName == 'abc123' | |
when: 'setFancyName is triggered' | |
widget.setFancyName('def987') | |
then: 'the fancyName is set' | |
widget.fancyName == 'def987' | |
and: 'the fancyNameLabel was set' | |
//Because fancyNameLabel is a @UiField it gets mocked, so we just verify it was called | |
Mockito.verify(widget.fancyNameLabel).setText('def987') | |
} | |
void "onOtherFieldKeyUp function sets fancyName to empty"() { | |
when: 'onOtherFieldKeyUp is triggered' | |
widget.onOtherFieldKeyUp([:] as KeyUpEvent) | |
then: 'the fancyName was cleared' | |
//Just simply verify the widget Spock spy method call | |
1 * widget.setFancyName('') | |
} | |
} |
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
AppController appController = Mock() | |
ErrorWidget errorWidget = Mock() | |
def widget | |
void setup() { | |
GwtMockito.initMocks(this) | |
//Requires Spock 1.2 to Spy instantiated object without a no-args constructor | |
// https://github.com/spockframework/spock/issues/769 | |
widget = Spy(new GwtSpockWidget(appController, errorWidget) { | |
void initWidget(Widget w) { /*disarming for test per gwtmockito docs */ } | |
}) | |
} | |
void cleanup() { | |
GwtMockito.tearDown() | |
} |
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
void "initialize successfully"() { | |
expect: 'the widget initialized' | |
widget | |
} |
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
void "setFancyName function sets fancyName and label"() { | |
given: 'fancyName with a value' | |
widget.fancyName = 'abc123' | |
and: 'a sanity check that the given values were set correctly' | |
assert widget.fancyName == 'abc123' | |
when: 'setFancyName is triggered' | |
widget.setFancyName('def987') | |
then: 'the fancyName is set' | |
widget.fancyName == 'def987' | |
and: 'the fancyNameLabel was set' | |
//Because fancyNameLabel is a @UiField it gets mocked, so we just verify it was called | |
Mockito.verify(widget.fancyNameLabel).setText('def987') | |
} |
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
void "onOtherFieldKeyUp function sets fancyName to empty"() { | |
when: 'onOtherFieldKeyUp is triggered' | |
widget.onOtherFieldKeyUp([:] as KeyUpEvent) | |
then: 'the fancyName was cleared' | |
//Just simply verify the widget Spock spy method call | |
1 * widget.setFancyName('') | |
} |
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
repositories { | |
maven { | |
//Required until Spock 1.2 is released out of snapshot | |
url 'https://oss.sonatype.org/content/repositories/snapshots/' | |
} | |
} | |
dependencies { | |
//Spock 1.2 needed to Spy instantiated objects without a no-args constructor | |
// https://github.com/spockframework/spock/issues/769 | |
testCompile('org.spockframework:spock-core:1.2-groovy-2.4-SNAPSHOT') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment