Spock 模拟异常导致代码覆盖率问题

问题描述 投票:0回答:1

我有以下代码,我正在尝试在 Spock 中进行测试

Class Validator{
    String envCheck(String environment) {
        if (environment == null || environment.isEmpty()) {
            new CustomException(steps).errorThrowException("ENVIRONMENT CANNOT BE EMPTY OR NULL")
        }
        environment = (environment.equalsIgnoreCase('Test')) ? 'UAT' : environment
       return environment
    }
    
}
class ValidatorTest extends Specification {
    def "validateEnvironment"() {
        given:
        Validator val = new Validator()
        CustomException mockException = GroovyMock(class: 'CustomException', global: true)
        when:
        envCheck(null)
        then:
        1 * new CustomException(*_) >> mockException
        1 * mockException.errorThrowException("ENVIRONMENT CANNOT BE EMPTY OR NULL")>>{ throw new IllegalStateException("ENVIRONMENT CANNOT BE EMPTY OR NULL") }
        thrown(IllegalStateException)
    }
}
Class CustomException {
    Object steps

    CustomException(final Object steps){
        this.steps = steps
    }

    void errorThrowException(String error) {
        printError(error)
        throw new IllegalStateException(error)
    }

    void printError(String error){
            this.steps.ansiColor('xterm') {
            this.steps.echo("""\033[3mERROR: $error \033[0m""")
        }
    }

}

当我运行此测试时,它执行正常,但在 jacoco 覆盖率报告中表示 if 条件未覆盖。当我直接将代码更改为

throw exception "throw new IllegalStateException("ENVIRONMENT CANNOT BE EMPTY")"
并删除测试中的模拟并仅使用测试中抛出的内容,jacoco 覆盖率显示为覆盖并且为绿色。我该如何解决这个问题。 CustomException.errorThrowException 在内部抛出 IllegalStateException。但是当模拟代码覆盖率没有正确出现时。

unit-testing groovy mocking spock
1个回答
0
投票

发布甚至无法编译的伪代码没有帮助,例如

Class
用大写“C”代替
class
。此外,在类
Validator
中,您使用
new CustomException(steps)
,但
steps
此时未定义。

此外,你不能只叫

envCheck(null)
,而应该是
val.envCheck(null)
。 Groovy Mock 的
class
参数在这里也是不必要的。

如果我解决了这些问题,测试就会通过。

这是我的MCVE

package de.scrum_master.stackoverflow.q78861028

class Validator {
  String envCheck(String environment) {
    if (environment == null || environment.isEmpty())
      new CustomException("XXX").errorThrowException("ENVIRONMENT CANNOT BE EMPTY OR NULL")
    environment = (environment.equalsIgnoreCase('Test')) ? 'UAT' : environment
    return environment
  }
}
package de.scrum_master.stackoverflow.q78861028

class CustomException {
  Object steps

  CustomException(final Object steps) {
    this.steps = steps
  }

  void errorThrowException(String error) {
    printError(error)
    throw new IllegalStateException(error)
  }

  void printError(String error) {
    this.steps.ansiColor('xterm') {
      this.steps.echo("""\033[3mERROR: $error \033[0m""")
    }
  }
}
package de.scrum_master.stackoverflow.q78861028

import spock.lang.Specification

class ValidatorTest extends Specification {
  def "validateEnvironment"() {
    given:
    Validator validator = new Validator()
    CustomException customException = GroovyMock(global: true)

    when:
    validator.envCheck(null)

    then:
    1 * new CustomException(*_) >> customException
    1 * customException.errorThrowException("ENVIRONMENT CANNOT BE EMPTY OR NULL") >> { throw new IllegalStateException("ENVIRONMENT CANNOT BE EMPTY OR NULL") }
    thrown(IllegalStateException)
  }
}

Groovy Web Console 中尝试一下。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.