Java 如何使用 Spock 测试 Grails 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18644581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:47:44  来源:igfitidea点击:

How to test Grails service using Spock?

javaunit-testinggrailsgroovyspock

提问by prayagupd

I have a EncouragementService.groovywith following method

我有EncouragementService.groovy以下方法

  class EncouragementService {
   def stripePaymentService 

   def encourageUsers(List<User> users){
     if(null != users && users.size()>0){
      for(User user : users){
        //logic
        stripePaymentService.encourage(user)
        //
      }
     }   
   }
  }

To test above code in JAVA universe, using JUnit I would first create two or three users in setup. Pass the list of users to encourageUsers(...)method and check whatever I want with the result.

为了在 JAVA Universe 中测试上述代码,使用 JUnit 我首先会在 setup 中创建两个或三个用户。将用户列表传递给encourageUsers(...)方法并检查我想要的结果。

How can I achieve the same thing here in grails,

我怎样才能在 grails 中实现同样的目标,

import com.github.jmkgreen.morphia.Datastore;

@TestFor(EncouragementService)
class EncouragementServiceSpec {

  def morphiaService = new MorphiaService()

  void testEncourageUsers() {
        List<User> users = createUsers();
        encouragementService.(users)  
            //
  }

  def createUsers(){
       Datastore datastore = morphiaService.dataStoreInstance()
       def user = new User()
       user.setName("Prayag Upd")
       //
       datastore.save(user)
       [user]
  }
}

I am using spock:0.7

我正在使用spock:0.7

plugins {
    test(":spock:0.7") { exclude "spock-grails-support" }
}

采纳答案by dmahapatro

Service class can be optimized as below:

服务类可以优化如下:

class EncouragementService {
   def encourageUsers(List<User> users){
       if(users){ //Groovy truth takes care of all the checks
          for(user in users){
            //logic
          }
       }   
   }
}

Spock Unit Test:
Spock takes testing to whole another level, where you can test the behavior (adheres to BDD). The test class would look like:

Spock 单元测试:
Spock 将测试提升到另一个级别,您可以在其中测试行为(遵循 BDD)。测试类看起来像:

import spock.lang.*

@TestFor(EncouragementService)
@Mock(User) //If you are accessing User domain object.
class EncouragementServiceSpec extends Specification{
  //def encouragementService //DO NOT NEED: mocked in @TestFor annotation

  void "test Encourage Users are properly handled"() {
      given: "List of Users"
          List<User> users = createUsers()

      when: "service is called"
          //"service" represents the grails service you are testing for
          service.encourageUsers(users) 

      then: "Expect something to happen"
          //Assertion goes here
  }

  private def createUsers(){
       return users //List<User>
  }
}

回答by Nicholas

Use the build-test-data pluginto build the users.

使用build-test-data 插件来构建用户。

@TestFor(EncouragementService)
@Build(User)
class EncouragementServiceSpec extends Specification {

  def "encourage users does x"() {
    given:
    def users = createUsers();

    when:
    service.encourageUsers(users)  

    then:
    // assert something
  }

  def createUsers(){
    [User.build(), User.build(), User.build()]
  }
}

I've also made a couple of changes to the code to make it a proper spock specification. Your test has to extend Specification and you might want to familiarize yourself with Spock's keywords.

我还对代码进行了一些更改,使其成为适当的 spock 规范。您的测试必须扩展 Specification 并且您可能想要熟悉Spock 的关键字