Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Last active April 30, 2018 21:27
Show Gist options
  • Save jeffsheets/02b3d77293c8f8419c0b31b47b627676 to your computer and use it in GitHub Desktop.
Save jeffsheets/02b3d77293c8f8419c0b31b47b627676 to your computer and use it in GitHub Desktop.
GWT Spock test with GwtMockito for Google Web Toolkit client testing
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('')
}
}
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('')
}
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