mockmvc

MockMvc to test SpringMVC Rest Web Service using JUnit, Mockito, and Hamcrest

有些话、适合烂在心里 提交于 2019-12-11 05:49:51
问题 Am using Spring MVC to create Restful Web Services... Here's my pom.xml: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.3.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.3.RELEASE</version> <

@ModelAttribute controller spring-mvc mocking

家住魔仙堡 提交于 2019-12-10 17:29:56
问题 I want to test a controller which is using @ModelAttribute for one of its method arguments. public String processSaveAction(@ModelAttribute("exampleEntity") ExampleEntity exampleEntity) @ModelAttribute method getExampleEntity is using @RequestParam : @ModelAttribute("exampleEntity") public ExampleEntity getExampleEntity(@RequestParam(value = "id", required = true) ExampleEntity exampleEntity) { My controller is using WebDataBinder to call a factory, which returns an object based on param "id"

Upload file using Spring mvc and MockMVC

戏子无情 提交于 2019-12-10 01:48:23
问题 I have successfully uploaded a image file to WebContent\resources\uploads\image.jsp . But I am facing a problem in testing this using MockMvc. When I run the test case I am getting the exceptions file not found and access denied . The controller looks like this: @RequestMapping(value="/AddContacts", method=RequestMethod.POST) public @ResponseBody String addContacts(ContactBean cb,HttpServletRequest request,HttpServletResponse response,@RequestParam("upload") MultipartFile file) throws

Can I use Spring's MockMvc with Jersey resources?

假装没事ソ 提交于 2019-12-08 21:38:45
问题 I have successfully integrated Spring and Jersey via Spring Boot' starter POMs, and I have a couple Jersey endpoints. Now I'd like to unit test the resources. I can't seem to get MockMvc working. I get a 404 error when attempting to GET a resource endpoint. I know there is a Jersey test framework out there, but it appears to launch a server. I'm hoping to avoid "integration" type tests and keep this as simple as possible. Can I do this with MockMvc? 回答1: Unfortunately doesn't seem to be

How to PUT multipart/form-data using Spring MockMvc?

自作多情 提交于 2019-12-08 15:58:41
问题 I have controller's method with PUT method, which receives multipart/form-data: @RequestMapping(value = "/putIn", method = RequestMethod.PUT) public Foo updateFoo(HttpServletRequest request, @RequestBody Foo foo, @RequestParam("foo_icon") MultipartFile file) { ... } and I want to test it using MockMvc . Unfortunately MockMvcRequestBuilders.fileUpload creates essentially instance of MockMultipartHttpServletRequestBuilder which has POST method: super(HttpMethod.POST, urlTemplate, urlVariables)

How to print only the requests of the failed test with @AutoConfigureMockMvc?

旧城冷巷雨未停 提交于 2019-12-08 05:12:54
问题 In our project we are using @AutoConfigureMockMvc with printOnlyOnFailure left as default, true . This works fine and prints none of the requests… except if any test fails. At that point, it prints all requests from ALL tests that have been executed before. Although this might sometimes be useful, this may print an enormous amount of log, and if it happens on our CI server, the log gets truncated and we cannot even see which test failed (since the AssertionError is printed afterwards. Even

Authentication token passed to ControllerAdvice is null when running through MockMvc

限于喜欢 提交于 2019-12-07 10:39:05
问题 I am using Spring Boot 1.3, Spring 4.2 and Spring Security 4.0. I am running integration tests using MockMvc, for example: mockMvc = webAppContextSetup(webApplicationContext).build(); MvcResult result = mockMvc.perform(get("/")) .andExpect(status().isOk()) .etc; In my tests I am simulating a user login like this: CurrentUser principal = new CurrentUser(user); Authentication auth = new UsernamePasswordAuthenticationToken(principal, "dummypassword", principal.getAuthorities());

MockRestServiceServer simulate backend timeout in integration test

二次信任 提交于 2019-12-05 14:36:04
问题 I am writing some kind of integration test on my REST controller using MockRestServiceServer to mock backend behaviour. What I am trying to achieve now is to simulate very slow response from backend which would finally lead to timeout in my application. It seems that it can be implemented with WireMock but at the moment I would like to stick to MockRestServiceServer. I am creating server like this: myMock = MockRestServiceServer.createServer(asyncRestTemplate); And then I'm mocking my backend

Authentication token passed to ControllerAdvice is null when running through MockMvc

自古美人都是妖i 提交于 2019-12-05 13:54:23
I am using Spring Boot 1.3, Spring 4.2 and Spring Security 4.0. I am running integration tests using MockMvc, for example: mockMvc = webAppContextSetup(webApplicationContext).build(); MvcResult result = mockMvc.perform(get("/")) .andExpect(status().isOk()) .etc; In my tests I am simulating a user login like this: CurrentUser principal = new CurrentUser(user); Authentication auth = new UsernamePasswordAuthenticationToken(principal, "dummypassword", principal.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(auth); This works fine for my methods that are annotated with

mockMvc - Test Error Message

我是研究僧i 提交于 2019-12-05 10:51:42
问题 Does anybody have any tips, or does anybody know how I can test the "error message" returned by the HTTP response object? @Autowired private WebApplicationContext ctx; private MockMvc mockMvc; @Before public void setUp() throws Exception { mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build(); } Response: MockHttpServletResponse: Status = 200 Error message = null Headers = {Content-Type=[application/json;charset=UTF-8]} Content type = application/json;charset=UTF-8 回答1: You can use the