mockmvc

Spring Controller testing with MockMvc post method

南楼画角 提交于 2020-01-23 06:23:04
问题 I am trying to test a method of my controller in a Spring Boot application. This is a post endpoint, which gets an id in a request and it passes on this id to a service: @Slf4j @Controller public class AdministrationController { private final AdministrationService administrationService; @Autowired public AdministrationController(AdministrationService administrationService) { this.administrationService = administrationService; } @PostMapping("/administration") public @ResponseBody

No JSON content (comes up null) from MockMvc Test using Spring MVC and Mockito

徘徊边缘 提交于 2020-01-15 12:00:51
问题 Have a codebase which uses SpringMVC 4.0.3.RELEASE for Restful Web Services. Codebase contains a fully functional Restful Web Service which I can verify works using postman and curl. However, when trying to write a unit test for the particular Restful Web Service using MockMvc, I become blocked with trying to obtain the JSON content from the unit test. Am wondering if its a config issue or an issue where I am not creating a fake object correctly (since this doesn't rely on tomcat and is

jsonpath: No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes

烈酒焚心 提交于 2020-01-03 17:28:09
问题 i tried to read the content of the json with jsonPath and i get an error. Here the junit test method: mockMvc.perform(get("/path") .andExpect(status().isOk()) .andExpect(jsonPath("$.id", is(1))) .andExpect(jsonPath("$.name", is("NAME"))) .andReturn().getResponse().getContentAsString(); here is what the request return me: [{"id":1,"name":"NAME","....}, ....}] i got this error: No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes. can

Spring MVC testing (security Integration test), JSESSIONID is not present

雨燕双飞 提交于 2020-01-02 02:38:29
问题 I have created custom login form for my spring boot app. In my form integration test, I want to check that received cookies contain JSESSIONID and XSRF-TOKEN . But, I received only XSRF-TOKEN . Here is my test: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest("server.port:0") public class UserIT { @Autowired private WebApplicationContext context; @Autowired private FilterChainProxy

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit

余生长醉 提交于 2019-12-29 08:29:09
问题 I am trying to get my feet wet with TDD. I am trying to write unit test cases for controllers using Mockito in conjunction with MockMvc and Junit. But I am getting a runtime error thereby failing my test. At first I was facing problem in initializing the MockMvc instance in the setup due to failure in finding the javax.servlet.SessionCookieConfig . This I resolved by downloading the javax.servlet api and configuring it into the build path of the project but then I am facing the java.lang

Spring integration test with mockmvc throws org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

风流意气都作罢 提交于 2019-12-24 11:34:52
问题 I have a weird problem with a spring integrationtest. I have a working controller and application that runs on tomcat. However in my integration test I get the following stacktrace: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:651) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker

Spring security not calling my custom authentication filter when running JUnit tests

跟風遠走 提交于 2019-12-22 04:31:10
问题 I'm trying to implement custom stateless authentication with Spring Security by following this article The problem I'm facing is that my custom filter is not being called by the framework, even when my SecurityConfig looks almost the same as in the previous link (a bit simpler): @Configuration @EnableWebMvcSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("appAuthenticationProvider") private

Rest Assured + Mock MVC @ControllerAdvice

浪尽此生 提交于 2019-12-21 05:55:08
问题 In my project I am using Rest Assured MockMVC with the following dependency: <dependency> <groupId>com.jayway.restassured</groupId> <artifactId>spring-mock-mvc</artifactId> <version>2.9.0</version> </dependency> And my test class looks like: TestController testController = new TestController(); @Before public void configureRestAssuredForController() { RestAssuredMockMvc.standaloneSetup(testController); } I have a couple of ExceptionHandlers defined in the controller class. In my JUnit tests I

JsonPath OR condition using MockMVC

久未见 提交于 2019-12-14 03:03:17
问题 I am practicing MockMVC for rest call unit testing. how can we test the Boolean values so that whether the result is true or false I need to pass the test, I tried as follows, mockMvc.perform(get("/student/{Id}", 1L)). .andExpect(status().isOk()) .andExpect(jsonPath("$.isPass", is(true || false))); Also I have list with 6 values, how can use list contains all kind of method, .andExpect(jsonPath("$.subjectList", hasSize(5))) .andExpect(jsonPath("$.subjectList.name", Matchers.contains("English"

How to resolve MethodArgumentConversionNotSupportedException with MockMvc?

爷,独闯天下 提交于 2019-12-11 13:18:54
问题 I'm writing a unit test for a controller method that accepts a MultipartFile and a custom object MessageAttachment . So far I can see that the MultipartFile is the correct format for the request but the MessageAttachment is not. The parsing of the messageAttachment throws a server side 500 error with MethodArgumentConversionNotSupportedException . It seem to be an issue with converting the MessageAttachment to a MockMultipartFile in the test. This is similar to the example shown here - https: