简体   繁体   中英

Disable global Spock Mock class for one test

Hello I have a global Mock in the setup method but would like to disable it for one test in the same class. Is it possible?

class Test extends Specification {

  void setup() {
    GroovyMock(Utils, global: true)
    Utils.getRemoteBranches(*_) >> new ArrayList<String>()
  }

  void "test1"() {
    given:
    Object context = getContext()

    when:
    ...

You could just rename your setup method, and call it in your given step. There is also a setup keyword in Spock. It has the same meaning as given .

For example, like this:

class Test extends Specification {

  void setupMock() {
    GroovyMock(Utils, global: true)
    Utils.getRemoteBranches(*_) >> new ArrayList<String>()
  }

  void "testWithMock"() {
    given:
    setupMock()
    Object context = getContext()

    when:
    // ...

  void "testWithoutMock"() {
    given:
    Object context = getContext()

    when:
    // ...

  }

In a way I agree to the comment regarding test readbility, if the tests without Mock are relatively important you should consider creating a separate Specification .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM