Skip to content

Instantly share code, notes, and snippets.

@alexfdz
Created June 8, 2013 12:48
Show Gist options
  • Save alexfdz/5735078 to your computer and use it in GitHub Desktop.
Save alexfdz/5735078 to your computer and use it in GitHub Desktop.
Cucumber + Spring MVC Test: Multiple step definitions entities with a shared context
@WebAppConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class CustomerStepDefinitions {
@Autowired
protected StepDefinitionsContext context;
@When("^I search for all the exisiting resources and format \"([^\"]*)\"$")
public void I_search_for_all_the_exisiting_resources_and_format(String mediaType) throws Throwable {
MediaType requestedMediatype = MediaType.parseMediaTypes(mediaType).get(0);
context.perform(
get(CUSTOMER_CONTROLLER_URI)
.accept(requestedMediatype));
}
....
}
@WebAppConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class RestStepDefinitions{
@Autowired
protected StepDefinitionsContext context;
@Then("^the response fails with a not found error$")
public void the_response_fails_with_a_not_found_error() throws Throwable {
context.andExpect(status().isNotFound());
}
@Then("^the response fails with an Not acceptable error$")
public void the_response_fails_with_an_Not_acceptable_error() throws Throwable {
context
.andExpect(status().isNotAcceptable());
}
...
}
@Component
public class StepDefinitionsContext implements ResultActions{
@Autowired
protected MockMvc mockMvc;
protected ResultActions currentResultAction;
public ResultActions getCurrentResultAction() {
return currentResultAction;
}
public void setCurrentResultAction(ResultActions currentResultAction) {
this.currentResultAction = currentResultAction;
}
@Override
public ResultActions andExpect(ResultMatcher matcher) throws Exception {
return currentResultAction.andExpect(matcher);
}
@Override
public ResultActions andDo(ResultHandler handler) throws Exception {
return currentResultAction.andDo(handler);
}
@Override
public MvcResult andReturn() {
return currentResultAction.andReturn();
}
public ResultActions perform(RequestBuilder requestBuilder) throws Exception {
setCurrentResultAction(mockMvc.perform(requestBuilder));
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment