mockmvc

MockMvc WebAppConfiguration: Load servlet mappings in web.xml

≯℡__Kan透↙ 提交于 2019-11-30 18:32:24
问题 I'm writing integration tests using MockMvc, and I'm wondering if there's a way to load servlet mappings from web.xml (which normally shouldn't matter). I have a custom HandlerInteceptor that matches the request URI (from HttpServletRequest ) against a template (using AntPathMatcher ). In web.xml, I define servlet mappings like this (with a corresponding mobile-context.xml): <servlet-mapping> <servlet-name>mobileServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>

Run unit tests on controllers that require authentication

不想你离开。 提交于 2019-11-30 15:04:41
问题 I have a spring boot application which requires login for some actions. I am trying to test them using MockMvc , but it doesn't seem to work. I keep getting a HTTP response with status 403 (forbidden). Probably there is something wrong with the authentication part. I have tried following the documentation, but I wasn't able to get it working. This is my current testing code: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {Application.class})

Register @ControllerAdvice annotated Controller in JUnitTest with MockMVC

夙愿已清 提交于 2019-11-30 11:02:06
My @ControllerAdvice annotated Controller looks like this: @ControllerAdvice public class GlobalControllerExceptionHandler { @ResponseStatus(value = HttpStatus.UNAUTHORIZED) @ExceptionHandler(AuthenticationException.class) public void authenticationExceptionHandler() { } } Of course my development is test driven and I would like to use my exception Handler in the JUnit Tests. My Test case looks like this: public class ClientQueriesControllerTest { private MockMvc mockMvc; @InjectMocks private ClientQueriesController controller; @Mock private AuthenticationService authenticationService; @Before

RestFuse vs Rest Assured vs MockMVC Rest Service Unit Test Framework

南楼画角 提交于 2019-11-30 03:15:01
问题 I've been trying to find a simple all purpose unit test framework for Spring MVC based Rest Services I've written. I've been searching online and narrowed it down to: RestFuse (http://developer.eclipsesource.com/restfuse/) Rest Assured (https://github.com/jayway/rest-assured) MockMVC (http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/) I like RestFuse because it's mostly annotation based, but rest assured seems to have a easier way of

Register @ControllerAdvice annotated Controller in JUnitTest with MockMVC

拈花ヽ惹草 提交于 2019-11-29 16:29:12
问题 My @ControllerAdvice annotated Controller looks like this: @ControllerAdvice public class GlobalControllerExceptionHandler { @ResponseStatus(value = HttpStatus.UNAUTHORIZED) @ExceptionHandler(AuthenticationException.class) public void authenticationExceptionHandler() { } } Of course my development is test driven and I would like to use my exception Handler in the JUnit Tests. My Test case looks like this: public class ClientQueriesControllerTest { private MockMvc mockMvc; @InjectMocks private

Why does Spring MockMvc result not contain a cookie?

Deadly 提交于 2019-11-29 09:20:27
I am trying to unit-test login and security in my REST API, so I try to mock real-life request sequences as close as possible. My first request would be: this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac). addFilters(springSecurityFilterChain).build(); this.mapper = new ObjectMapper(); .... MvcResult result=mockMvc.perform(get("/login/csrf")).andExpect(status().is(200)).andReturn(); Cookie[] cookies = result.getResponse().getCookies(); (See full class on pastebin ). I try to get the cookie here to be able to login with the received CSRF token later, but the cookies array is empty!

Why does Spring MockMvc result not contain a cookie?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 02:46:43
问题 I am trying to unit-test login and security in my REST API, so I try to mock real-life request sequences as close as possible. My first request would be: this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac). addFilters(springSecurityFilterChain).build(); this.mapper = new ObjectMapper(); .... MvcResult result=mockMvc.perform(get("/login/csrf")).andExpect(status().is(200)).andReturn(); Cookie[] cookies = result.getResponse().getCookies(); (See full class on pastebin). I try to get the