mockmvc

Validate LocalDate in JSON response with Spring MockMVC

我只是一个虾纸丫 提交于 2021-02-10 05:56:48
问题 I'm trying to validate a LocalDate object in a JSON result returned by a Spring MVC webservice but I can't figure out how. At the moment I always run into assertion errors like the following one: java.lang.AssertionError: JSON path "$[0].startDate" Expected: is <2017-01-01> but: was <[2017,1,1]> The important part of my test is posted below. Any ideas how to fix the test to pass? import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; public class

java.lang.AssertionError: Content type not set - Spring Controller Junit Tests

房东的猫 提交于 2021-02-07 12:14:29
问题 I am trying to do some unit testing on my controllers. No matter what I do all controller tests return java.lang.AssertionError: Content type not set I am testing that the methods return json and xml data. Here is an example of the controller: @Controller @RequestMapping("/mypath") public class MyController { @Autowired MyService myService; @RequestMapping(value="/schema", method = RequestMethod.GET) public ResponseEntity<MyObject> getSchema(HttpServletRequest request) { return new

java.lang.AssertionError: Content type not set - Spring Controller Junit Tests

▼魔方 西西 提交于 2021-02-07 12:14:21
问题 I am trying to do some unit testing on my controllers. No matter what I do all controller tests return java.lang.AssertionError: Content type not set I am testing that the methods return json and xml data. Here is an example of the controller: @Controller @RequestMapping("/mypath") public class MyController { @Autowired MyService myService; @RequestMapping(value="/schema", method = RequestMethod.GET) public ResponseEntity<MyObject> getSchema(HttpServletRequest request) { return new

Testing non-blocking REST services with Spring MVC

梦想与她 提交于 2021-01-28 07:42:09
问题 I have a Spring MVC application . I want to test this controller: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("classpath:backoffice-servlet.xml") public class TimeControllerTests { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void should_OK() throws Exception { mockMvc.perform(get("/time/2") .contentType(APPLICATION

MockMVC in junit tests - checking result for List<Object> and Map<Enum, Object>

Deadly 提交于 2020-08-27 07:51:29
问题 I have problems with mockMVC and test written with that. My tests fails. I have two tests methods: @Test public void getPersonsForApiConsumerTest() throws Exception { mockMvc.perform(get(getUri("/consumers/1/persons"))) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[1].name", is("Ligza"))) .andExpect(jsonPath("$[2].name", is("Vekrir"))); } @Test public void getPersonsForApiConsumerMapTest() throws Exception { mockMvc.perform(get(getUri("/consumers/1

SpringBoot @WebMvcTest security issue

淺唱寂寞╮ 提交于 2020-05-11 05:36:28
问题 I have a spring rest mvc controller which has the url "/public/rest/vehicle/get". In my security configuration, I have defined that any requests for /public/rest should not require authentication. http. csrf().disable() .authorizeRequests() .antMatchers("/home/**", "/**", "/css/**", "/js/**", "/fonts/**", "/images/**", "/public/rest/**","/login*","/signin/**","/signup/**").permitAll() .antMatchers("/property/**").authenticated() .and() .formLogin().loginPage("/login").permitAll() .and()

Unit test Springboot MockMvc returns 403 Forbidden

て烟熏妆下的殇ゞ 提交于 2020-01-24 06:22:26
问题 I wrote one unit test that tests UsersController. UsersControllerTest.findUser is working fine, but UsersControllerTest.insertGetModifyDelete it doesn't. In the log of the test I can see that the POST request doesn't match any method of UsersController, but I don't understand why. Could you help me with this, please?. This is my rest java class: @RestController @RequestMapping("/users") public class UsersController { private final UsersService usersService; @Autowired public UsersController