Comments (7)
Yes, we probably could do some such option. It wouldn't work well with JSP but apart from that yes.
from spring-mvc-showcase.
MockMvc
is supposed to be immutable, so that API wouldn't really work.
Perhaps .andPerformForward()
returning a new ResultActions
, if that's possible.
mockMvc.perform(get("/one"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/two"))
.andPerformForward()
.andExpect(content().string(...))
Or on the original builder somehow
mockMvc.perform(get("/one").with(followForwards()))
I see a ForwardRequestPostProcessor
but that needs to know the forwarded URL on construction.
from spring-mvc-showcase.
Something like this?
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
from spring-mvc-showcase.
That would be good. I also want to test the actual output from the forwarded resource (it's an API and the rest-docs should contain the proper response). Is there a way to do that? I think you showed me how to do once but I must have lost the knack.
from spring-mvc-showcase.
At the moment we only have an explicit option for async dispatches. Should probably add one for FORWARD and ERROR dispatches. Take a look at how MockMvcRequestBuilders#asyncDispatch is done.
from spring-mvc-showcase.
Something like this?
public static RequestBuilder forward(MvcResult result) {
return new RequestBuilder() {
@Override
public MockHttpServletRequest buildRequest(
ServletContext servletContext) {
MockHttpServletRequest request = result.getRequest();
request.setRequestURI(result.getResponse().getForwardedUrl());
return request;
}
};
}
and then
@Test
public void getLocations() throws Exception {
final MvcResult result = this.mockMvc
.perform(MockMvcRequestBuilders.get("/")).andReturn();
this.mockMvc.perform(forward(result))
.andExpect(...);
}
It seems clunky to me, and I'm not sure I care too much about shaving this yak. Would it not be possible to build this into the perform()
method? E.g. as a global option:
@Test
public void getLocations() throws Exception {
this.mockMvc.followForwards(true);
this.mockMvc
.perform(MockMvcRequestBuilders.get("/")).andExpect(...);
}
from spring-mvc-showcase.
Also, although the test passes, it doesn't work with restdocs (no snippets created). Cc @wilkinsona.
UPDATE: cancel that, it works fine with restdocs. It's just the yak shaving that I would like to fix.
from spring-mvc-showcase.
Related Issues (20)
- Not able to unit test validations using MockMVC HOT 1
- JavaBean implements Serializable HOT 1
- HTTP Status 500 - Servlet.init() for servlet appServlet threw exception HOT 4
- JPA addition HOT 2
- no .classpath,then how to import to eclipse? HOT 2
- Timezone configuration makes tests fail HOT 2
- isMovedTemporarily() is @Deprecated HOT 1
- Error on pom.xml HOT 7
- Unable to run spring-mvc-showcase in STS 3.8.1 HOT 1
- Simple Question
- HTTP Status 403 - Expected CSRF token not found. Has your session expired?
- Project can't run on the server, showing an error of catalina.policy and catalina.properties HOT 2
- Project has been tested on tomcat -8.5.24 HOT 1
- j2ee maven plugin could be used replacing jstl , jsp , servlet maven dependency HOT 1
- 1
- 404 autowired controller fail
- [ERROR] COMPILATION ERROR HOT 3
- HTTP Status 500 - Servlet.init() for servlet dispatcher threw exception HOT 1
- Null pointer exception while server startup HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-mvc-showcase.