Created
June 8, 2013 12:48
-
-
Save alexfdz/5735078 to your computer and use it in GitHub Desktop.
Cucumber + Spring MVC Test: Multiple step definitions entities with a shared context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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)); | |
} | |
.... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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()); | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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