spring-test-mvc

How to check JSON in response body with mockMvc

无人久伴 提交于 2019-11-29 15:57:34
问题 This is my method inside my controller which is annotated by @Controller @RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8") @ResponseBody public JSONObject getServerAlertFilters(@PathVariable String serverName) { JSONObject json = new JSONObject(); List<FilterVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, ""); JSONArray jsonArray = new JSONArray(); jsonArray.addAll(filteredAlerts); json.put(SelfServiceConstants

java.lang.AssertionError: Content type not set while junit Spring MVC Controller?

给你一囗甜甜゛ 提交于 2019-11-29 07:12:47
I am using JUnit to test my Spring MVC controller. Below is my method which returns a index.jsp page and shows Hello World on the screen - @RequestMapping(value = "index", method = RequestMethod.GET) public HashMap<String, String> handleRequest() { HashMap<String, String> model = new HashMap<String, String>(); String name = "Hello World"; model.put("greeting", name); return model; } And below is my JUnit test for the above method: public class ControllerTest { private MockMvc mockMvc; @Before public void setup() throws Exception { InternalResourceViewResolver viewResolver = new

Does the new Spring MVC Test Framework released in Spring 3.2 test the web.xml configuration?

﹥>﹥吖頭↗ 提交于 2019-11-29 01:43:11
问题 I've read the docs ( http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework ) several times and I can't confirm if the WebApplicationContext context that gets injected when you use the @WebApplicationContext annotation is actually looking at the web.xml. In other words, I want to test my web.xml configuration. The filters and servlet path specifically. But when I configure my test it ignores the web.xml. (e.g. I try a get

Are Spring's MockMvc used for unit testing or integration testing?

坚强是说给别人听的谎言 提交于 2019-11-28 16:53:26
问题 Spring has 2 setups for the MockMvc: Standalone setup WebApplicationContext setup In general what kind of testing is MockMvc used for? Unit or Integration? or Both? Am i right in saying that using the standalone setup (running outside the Spring's application context) allows you to write unit tests and with the WebApplicationContext setup you can write integration tests? 回答1: Both forms are actually integration tests since you are testing the integration of your code with the Spring

count members with jsonpath?

久未见 提交于 2019-11-28 15:13:48
问题 Is it possible to count the number of members using JsonPath? Using spring mvc test I'm testing a controller that generates {"foo": "oof", "bar": "rab"} with standaloneSetup(new FooController(fooService)).build() .perform(get("/something").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("$.foo").value("oof")) .andExpect(jsonPath("$.bar").value("rab")); I'd like to make sure that no other members are present in the generated json. Hopefully by counting them

How to write a unit test for a Spring Boot Controller endpoint

旧巷老猫 提交于 2019-11-28 03:18:43
I have a sample Spring Boot app with the following Boot main class @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } Controller @RestController @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/") String gethelloWorld() { return "Hello World!"; } } What's the easiest way to write a unit test for the controller? I tried the following but it complains about failing to autowire WebApplicationContext @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes =

Spring MVC integration tests with Spring Security

北城以北 提交于 2019-11-27 20:22:32
问题 I'm trying to test my login page using mvc-test. I was working pretty good before I added spring security. My code is: mockMvc.perform( post("j_spring_security_check") .param(LOGIN_FORM_USERNAME_FIELD, testUsernameValue) .param(LOGIN_FORM_PASSWORD_FIELD, testPasswordValue)) .andDo(print()) .andExpect(status().isOk()) .andExpect(model().attribute(LOGIN_PAGE_STATUS_VALUE, LOGIN_PAGE_STATUS_FALSE_INDICATOR)); Test class has correct annotations added: @RunWith(SpringJUnit4ClassRunner.class)

Testing security in Spring Boot 1.4

强颜欢笑 提交于 2019-11-27 16:08:53
I'm trying to test @WebMvcTest with custom security settings defined in SecurityConfig class: @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/admin*").access("hasRole('ADMIN')").antMatchers("/**").permitAll().and().formLogin(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("user").password("password").roles("ADMIN"); } } Test class is: @RunWith

How to check String in response body with mockMvc

↘锁芯ラ 提交于 2019-11-27 10:08:52
I have simple integration test @Test public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception { mockMvc.perform(post("/api/users").header("Authorization", base64ForTestUser).contentType(MediaType.APPLICATION_JSON) .content("{\"userName\":\"testUserDetails\",\"firstName\":\"xxx\",\"lastName\":\"xxx\",\"password\":\"xxx\"}")) .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(?); } In last line I want to compare string received in response body to expected string And in response I get: MockHttpServletResponse: Status = 400 Error message = null

How to write a unit test for a Spring Boot Controller endpoint

余生长醉 提交于 2019-11-27 05:06:03
问题 I have a sample Spring Boot app with the following Boot main class @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } Controller @RestController @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/") String gethelloWorld() { return "Hello World!"; } } What's the easiest way to write a unit test for the controller? I tried the following but it complains about failing to autowire