Spock 异常交互失败但 mockito 异常交互有效?
Spock exception interaction fails but mockito exception interaction works?
我在 Spock 规范和异常处理方面遇到问题。
我有一些调用服务并捕获特定类型异常的代码。在 catch 块中抛出另一种类型的异常:
try {
int result = service.invoke(x,y);
...
} catch (IOException e) {
throw BusinessException(e);
}
以下测试用例使用 mockito 模拟工作:
given: "a service that fails"
def service = mock(Service)
when(service.invoke(any(),any())).thenThrow(new IOException())
and: "the class under test using that service"
def classUnderTest = createClassUnderTest(service)
when: "the class under test does something"
classUnderTest.doSomething()
then: "a business exception is thrown"
thrown(BusinessException)
所有测试都已通过
但在使用 Spock 处理服务交互时,以下测试用例失败:
given: "a service that fails"
def service = Mock(Service)
service.invoke(_,_) >> { throw new IOException() }
and: "the class under test using that service"
def classUnderTest = createClassUnderTest(service)
when: "the class under test does something"
classUnderTest.doSomething()
then: "a business exception is thrown"
thrown(BusinessException)
应为 BusinessException 类型的异常,但未抛出异常
我不确定这里发生了什么。它适用于 mockito 但不适用于 Spock。
我没有验证抛出异常的服务,因此不需要在 when/then 块中设置它。
(使用 groovy-all:2.4.5,spock-core:1.0-groovy-2.4)
以下示例运行良好:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def "simple test"() {
given:
def service = Mock(Service) {
invoke(_, _) >> { throw new IOException() }
}
def lol = new Lol()
lol.service = service
when:
lol.lol()
then:
thrown(BusinessException)
}
}
class Service {
def invoke(a, b) {
println "$a $b"
}
}
class Lol {
Service service
def lol() {
try {
service.invoke(1, 2)
} catch(IOException e) {
throw new BusinessException(e)
}
}
}
class BusinessException extends RuntimeException {
BusinessException(Exception e) {}
}
也许您配置有误?
我在 Spock 规范和异常处理方面遇到问题。
我有一些调用服务并捕获特定类型异常的代码。在 catch 块中抛出另一种类型的异常:
try {
int result = service.invoke(x,y);
...
} catch (IOException e) {
throw BusinessException(e);
}
以下测试用例使用 mockito 模拟工作:
given: "a service that fails"
def service = mock(Service)
when(service.invoke(any(),any())).thenThrow(new IOException())
and: "the class under test using that service"
def classUnderTest = createClassUnderTest(service)
when: "the class under test does something"
classUnderTest.doSomething()
then: "a business exception is thrown"
thrown(BusinessException)
所有测试都已通过
但在使用 Spock 处理服务交互时,以下测试用例失败:
given: "a service that fails"
def service = Mock(Service)
service.invoke(_,_) >> { throw new IOException() }
and: "the class under test using that service"
def classUnderTest = createClassUnderTest(service)
when: "the class under test does something"
classUnderTest.doSomething()
then: "a business exception is thrown"
thrown(BusinessException)
应为 BusinessException 类型的异常,但未抛出异常
我不确定这里发生了什么。它适用于 mockito 但不适用于 Spock。
我没有验证抛出异常的服务,因此不需要在 when/then 块中设置它。
(使用 groovy-all:2.4.5,spock-core:1.0-groovy-2.4)
以下示例运行良好:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def "simple test"() {
given:
def service = Mock(Service) {
invoke(_, _) >> { throw new IOException() }
}
def lol = new Lol()
lol.service = service
when:
lol.lol()
then:
thrown(BusinessException)
}
}
class Service {
def invoke(a, b) {
println "$a $b"
}
}
class Lol {
Service service
def lol() {
try {
service.invoke(1, 2)
} catch(IOException e) {
throw new BusinessException(e)
}
}
}
class BusinessException extends RuntimeException {
BusinessException(Exception e) {}
}
也许您配置有误?