【发布时间】:2016-02-24 16:14:37
【问题描述】:
我正在与:
- Spock 核心
- Spock 报告
- 斯波克弹簧
- Spring MVC 测试
我有以下代码:
@FailsWith(java.lang.AssertionError.class)
def "findAll() Not Expected"(){
given:
url = PersonaUrlHelper.FINDALL;
when:
resultActions = mockMvc.perform(get(url)).andDo(print())
then:
resultActions.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_XML))
}
这里的代码失败(预期如何),因为被测试的方法真的返回(MediaType.APPLICATION_JSON)而不是(MediaType.APPLICATION_XML)。
所以@FailsWith(java.lang.AssertionError.class)的原因。
即使我使用 @FailsWith(value=java.lang.AssertionError.class, reason="JSON returned ..."),我也无法通过 Spock 报告
reason
问题一:我如何在 Spock 报告上看到 reason?。
我知道 Spock 提供 throw() 方法,因此我可以这样做:
then:
def e = thrown(IllegalArgumentException)
e.message == "Some expected error message"
println e.message
遗憾的是 throw 不适用于 AssertionError。
如果我使用thrown(AssertionError),测试方法不会通过,唯一的方法是通过@FailsWith,但我无法从 AssertionError 获取错误消息
问题二如何从 AssertionError 中获取错误信息?
我知道我可以做类似的事情
then: "Something to show on Spock Reports"
只是好奇第二个问题能不能解决..
【问题讨论】:
标签: testing integration-testing spock spring-mvc-test spock-reports