Skip to content

Instantly share code, notes, and snippets.

@mhaligowski
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhaligowski/42d2b54c0fbd65e95d9d to your computer and use it in GitHub Desktop.
Save mhaligowski/42d2b54c0fbd65e95d9d to your computer and use it in GitHub Desktop.
package io.mhlg.spock.test
class SuperDuperService {
def superDuperUrl
def doTheMagic() {
superDuperUrl.text
}
}
package io.mhlg.spock.test
import spock.lang.Specification
import javax.swing.JTextField
class SuperDuperServiceSpec extends Specification {
def "should do the magic with groovy mock"() {
given:
def mockUrl = GroovyMock(URL)
def expectedString = "whoaaa, it works!"
mockUrl.text >> expectedString
def testObj = new SuperDuperService(superDuperObjectWithText: mockUrl)
when:
def result = testObj.doTheMagic()
then:
1 * mockUrl.text
result == expectedString
}
def "should do the magic with plain mock"() {
given:
def aMock = Mock(JTextField)
def expectedString = "whoaaa, it works!"
aMock.text >> expectedString
def testObj = new SuperDuperService(superDuperObjectWithText: aMock)
when:
def result = testObj.doTheMagic()
then:
1 * aMock.text
result == expectedString
}
def "should do the magic with plain mock [CORRECTLY]"() {
given:
def aMock = Mock(JTextField)
def testObj = new SuperDuperService(superDuperObjectWithText: aMock)
when:
def result = testObj.doTheMagic()
then:
1 * aMock.text >> "whoaaa, it works!"
result == "whoaaa, it works!"
}
def "should do the magic with groovy mock [CORRECTLY]"() {
given:
def mockUrl = GroovyMock(URL)
def expectedString = "whoaaa, it works!"
def testObj = new SuperDuperService(superDuperObjectWithText: mockUrl)
when:
def result = testObj.doTheMagic()
then:
1 * mockUrl.text >> expectedString
and:
result == expectedString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment