我对Spock规范和异常处理有问题。
我有一些调用服务并捕获特定类型异常的代码。在catch块中引发另一种类型的异常:
try {
int result = service.invoke(x,y);
...
} catch (IOException e) {
throw BusinessException(e);
}
以下测试用例使用mockito mock工作:
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)
预期的异常类型BusinessExcema',但没有引发异常
我不知道这里发生了什么。它对莫基托有效,但对斯波克无效。
我没有验证引发异常的服务,因此不需要在when/then块中设置它。
(使用groovy-all:2.4.5,spock核心: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) {}
}
也许你误解了什么?
我正在应用程序中使用Windows Azure SQL Server,当我连接到应用程序时,它显示以下错误。 hibernate.cfg.xml: .................. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cont
我有接口 接口的实现是 我还有一节课 我正在尝试为MyClass编写JUnit。我已经做了 但我需要mockito但没有调用,实际上在验证调用时与这个mock没有任何交互。 谁能提出一些解决方案。
问题内容: 在此代码中: 当有一个例外,从,都和运行,或者是通过异常无论是消费还是? 编辑: 返回,可能是。这是我所谈论的例外。 问题答案: 的文档说: 返回一个 具有与此阶段相同的结果或异常 的新CompletionStage ,该阶段 在此阶段完成时执行给定的操作。 (强调我的) 这意味着该阶段不会吞噬异常,因为它应该具有相同的结果或异常。但是,您可能通过这样的事实感到惊讶的是后续阶段将获得前
我试图登录到beeradvocate.com抓取(抓取)一些啤酒数据。我试过硒,但失败了。 这是html 我尝试使用名称、值和类,但一切都失败了。我最后一次尝试Xpath,但也失败了。 网站和检查 我的代码: 我已经使按钮工作: 然而,我需要能够执行发送_键来输入id和pw来登录。。。有人知道吗?
我有一个简单的类,它基于一个简单的规则构建一个。 如果在数据库中找到,则使用该元数据生成消息 这是这门课和一个斯波克测试。测试失败时,出现,表示无法找到使用该服务创建的。 我真的很想理解为什么这不起作用。我非常感谢在成功测试这项测试中得到的任何帮助。非常感谢。
我有一门课要考。 提供了我的测试类, 我得到了下面提供的错误堆栈, 需要但未调用:Holder.SetapIkey();->在com.ratepay.ella.service.config.EmailageServiceConfigurationTest.TestEmailageConfigholder(EmailageServiceConfigurationTest.java:48)实际上,与这