美文网首页
spock-framework 使用

spock-framework 使用

作者: JacobChan001 | 来源:发表于2020-08-15 23:43 被阅读0次

Mocking

// 测试类
UserService userService = new UserService()
// mock 类
def userDao = Mock(UserDao)
// inject mock 
userService.userDao = userDao

Stubing

// toNothing 不 stub 
// stub 返回值
userDao.get(_) >> user

// stub 方法异常
userDao.get(_) >> { throw new InternalError("ouch") }

// 多次调用不同处理
// 第一次调用时返回 user1, 第二次返回 user2
userDao.get(_) >>> {user1, user2}

Assertion

// assert value
when:
  def resutl = userService.getUser("user-id")
then:
  result.id == "user-id"

// assert invocation times
when:
  def result = userService.getUser("user-id")
then:
  1* userDao.get(_)

// assert mock method paramter
when:
  def result = userService.getUser("user-id")
then:
  1* userDao.get( {user.id == "user-id";
  user.name = "user-name"})

// assert exception

when:
  userService.getUser("user-id")
then:
  def e = throw(InternalError)
  e.message = "ouch"

maven -T

相关文章

网友评论

      本文标题:spock-framework 使用

      本文链接:https://www.haomeiwen.com/subject/utendktx.html