spring-test-mvc

How to pass parameters to post method in Spring MVC controller while doing junit?

孤人 提交于 2019-12-22 18:37:40
问题 I am doing unit testing on my Spring MVC controller methods. Below is my method which I am trying to unit test as it is working fine when I start my server. Whenever I will be hitting index page it will show me three text box on the browser in which I am typing the data and pressing the submit button and then the call goes to addNewServers with the proper values. Now I need to unit test the same thing: @RequestMapping(value = "/index", method = RequestMethod.GET) public Map<String, String>

Spring load testing

半城伤御伤魂 提交于 2019-12-22 08:19:14
问题 I've implemented a testing unit using spring (mock mvc), and I'm looking for a tool to run this unit in many threads/processes (so it will act as load testing for my server). I've seen applications like the grinder and jmeter but I don't want to re-write the entire unit, but just to use the existing one. Any ideas? 回答1: JMeter is able to execute existing JUnit tests via JUnit Request sampler, all you need to do is to drop jar(s) with your test along with dependencies somewhere in JMeter

pass remoteUser value in HttpServletRequest to mockmvc perform test

a 夏天 提交于 2019-12-21 20:23:58
问题 I have an api call as: @RequestMapping(value = "/course", method = RequestMethod.GET) ResponseEntity<Object> getCourse(HttpServletRequest request, HttpServletResponse response) throwsException { User user = userDao.getByUsername(request.getRemoteUser()); } I'm getting the user null when I call this from the test class like: HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getRemoteUser()).thenReturn("test1"); MvcResult result = mockMvc.perform( get( "

Testing Spring asyncResult() and jsonPath() together

会有一股神秘感。 提交于 2019-12-21 03:45:32
问题 I'm using a restful url to kick off a long-running backend process (it is normally on a cron schedule, but we want the ability to kick it off manually). The code below works and I see the result in the browser when I test manually. @ResponseBody @RequestMapping(value = "/trigger/{jobName}", method = RequestMethod.GET) public Callable<TriggerResult> triggerJob(@PathVariable final String jobName) { return new Callable<TriggerResult>() { @Override public TriggerResult call() throws Exception { /

Testing with spring-test-mvc jsonpath returns null

六眼飞鱼酱① 提交于 2019-12-19 04:04:19
问题 I am using Spring's "spring-test-mvc" library to test web controllers. I have a very simple controller that returns a JSON array. Then in my test I have: @Test public void shouldGetAllUsersAsJson() throws Exception { mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON)) .andExpect(content().mimeType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("fName").exists()); } The above test returns: java.lang.AssertionError: No value for JSON path: fName To quickly check what I

Use @WithMockUser (with @SpringBootTest) inside an oAuth2 Resource Server Application

岁酱吖の 提交于 2019-12-18 14:49:26
问题 Environment : I have a spring boot based microservice architecture application consisting of multiple infrastructural services and resource services (containing the business logic). Authorization and authentication is handled by an oAuth2-Service managing the user entities and creating JWT tokens for the clients. To test a single microservice application in its entirety i tried to build tests with testNG , spring.boot.test , org.springframework.security.test ... @SpringBootTest(webEnvironment

Spring MockMVC - How to mock custom validators running outside of controllers

北城以北 提交于 2019-12-18 07:12:49
问题 @UsernameAlreadyExists private String username; I have a custom validator that I created to ensure that duplicate usernames are caught by the application when account creation form submits. When I unit test the account creation controller using MockMVC, it fails as the validator depends on a service, so I get null pointer exception. How can I mock the validator or the service this validator depends on? I could not figure out how to make this work as the controller does not depend on the

Testing security in Spring Boot 1.4

混江龙づ霸主 提交于 2019-12-17 14:50:48
问题 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

How to check String in response body with mockMvc

时光毁灭记忆、已成空白 提交于 2019-12-17 10:07:53
问题 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

Test maximum upload file size with MockMultipartFile

最后都变了- 提交于 2019-12-12 10:39:10
问题 I create a file upload service with Spring Boot and test it with Spring Mock Mvc and MockMultipartFile. I want to test if an error is thrown when the maximum file size is exceeded. The following test fails because it receive a 200. RandomAccessFile f = new RandomAccessFile("t", "rw"); f.setLength(1024 * 1024 * 10); InputStream is = Channels.newInputStream(f.getChannel()); MockMultipartFile firstFile = new MockMultipartFile("data", "file1.txt", "text/plain", is); mvc.perform(fileUpload("/files