Spock 不适用于预期的 "matched invocations" 通过 @WebAppConfiguration 工作
Spock does not work for an expected "matched invocations" working through @WebAppConfiguration
我正在与
合作
- STS
- Gradle
- Spock 核心
- Spock 报告
- 史波克Spring
- Spring MVC 测试
我有以下测试代码:
@WebAppConfiguration
@ContextConfiguration(classes=[RootApplicationContextConfig.class,ServletApplicationContextConfig.class])
@SuppressWarnings("deprecation")
class PersonaXmlFindOneControllerTest extends Specification {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
private PersonaXmlFindOneController personaXmlFindOneController
def setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
personaXmlFindOneController = webApplicationContext.getBean(PersonaXmlFindOneController.class);
println personaXmlFindOneController.toString()
}
def "findOneRequestParamById deberia ser llamado"(){
String url = null
ResultActions resultActions = null
given: "The URL being used "
url = "some url to test"
when: "When the URL is being calling with a GET"
resultActions = mockMvc.perform(get(url, PersonaControllerSupport.ID)).andDo(print())
then: "...."
resultActions.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_XML))
.andExpect(xpath("persona").exists())
.andExpect(xpath("persona").nodeCount(1))
….
//then:
//1 * personaXmlFindOneController.findOneRequestParamById(_ as String)
}
代码运行良好。通过了。
此外:通过Gradle测试报告感谢andDo(print())我可以确认personaXmlFindOneController.findOneRequestParamById 已被调用。
意思是
Handler:
Type = com.manuel.jordan.controller.xml.PersonaXmlFindOneController
Method = public com.manuel.jordan.domain.xml.PersonaXml com.manuel.jordan.controller.xml.PersonaXmlFindOneController.findOneRequestParamById(java.lang.String)
现在如果启用
//then:
//1 * personaXmlFindOneController.findOneRequestParamById(_ as String)
代码失败,
Too few invocations for:
1 * personaXmlFindOneController.findOneRequestParamById(_ as String) (0 invocations)
Unmatched invocations (ordered by similarity):
None
观察在setup方法中,已经通过
获取到了
personaXmlFindOneController = webApplicationContext.getBean(PersonaXmlFindOneController.class);
因此,缺少什么或有什么问题?
您正在混合使用两种不同的模拟机制。
有 Spring 一个 (MockMVC) 和 Spock 一个。
Spock 只能验证自己创建的模拟(即使用 Spock Mock() 方法创建的模拟)。您没有在代码中创建任何 Spock 模拟,因此 Spock 模拟将不起作用。
请参阅 Spock 的官方文档以获取完整的模拟指南,以了解如何仅使用 Spock 创建模拟。
在您的特定示例中,您的原始代码是正确的,应该保持原样。您不必总是使用 Spock 模拟机制。只使用 Spring 测试设施的 Spock 测试是完全没问题的。
我正在与
合作- STS
- Gradle
- Spock 核心
- Spock 报告
- 史波克Spring
- Spring MVC 测试
我有以下测试代码:
@WebAppConfiguration
@ContextConfiguration(classes=[RootApplicationContextConfig.class,ServletApplicationContextConfig.class])
@SuppressWarnings("deprecation")
class PersonaXmlFindOneControllerTest extends Specification {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
private PersonaXmlFindOneController personaXmlFindOneController
def setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
personaXmlFindOneController = webApplicationContext.getBean(PersonaXmlFindOneController.class);
println personaXmlFindOneController.toString()
}
def "findOneRequestParamById deberia ser llamado"(){
String url = null
ResultActions resultActions = null
given: "The URL being used "
url = "some url to test"
when: "When the URL is being calling with a GET"
resultActions = mockMvc.perform(get(url, PersonaControllerSupport.ID)).andDo(print())
then: "...."
resultActions.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_XML))
.andExpect(xpath("persona").exists())
.andExpect(xpath("persona").nodeCount(1))
….
//then:
//1 * personaXmlFindOneController.findOneRequestParamById(_ as String)
}
代码运行良好。通过了。
此外:通过Gradle测试报告感谢andDo(print())我可以确认personaXmlFindOneController.findOneRequestParamById 已被调用。
意思是
Handler:
Type = com.manuel.jordan.controller.xml.PersonaXmlFindOneController
Method = public com.manuel.jordan.domain.xml.PersonaXml com.manuel.jordan.controller.xml.PersonaXmlFindOneController.findOneRequestParamById(java.lang.String)
现在如果启用
//then:
//1 * personaXmlFindOneController.findOneRequestParamById(_ as String)
代码失败,
Too few invocations for:
1 * personaXmlFindOneController.findOneRequestParamById(_ as String) (0 invocations)
Unmatched invocations (ordered by similarity):
None
观察在setup方法中,已经通过
获取到了personaXmlFindOneController = webApplicationContext.getBean(PersonaXmlFindOneController.class);
因此,缺少什么或有什么问题?
您正在混合使用两种不同的模拟机制。
有 Spring 一个 (MockMVC) 和 Spock 一个。
Spock 只能验证自己创建的模拟(即使用 Spock Mock() 方法创建的模拟)。您没有在代码中创建任何 Spock 模拟,因此 Spock 模拟将不起作用。
请参阅 Spock 的官方文档以获取完整的模拟指南,以了解如何仅使用 Spock 创建模拟。
在您的特定示例中,您的原始代码是正确的,应该保持原样。您不必总是使用 Spock 模拟机制。只使用 Spring 测试设施的 Spock 测试是完全没问题的。