Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.