spring-test-mvc

Test spring rest controller secured with https

痞子三分冷 提交于 2019-12-07 16:43:05
问题 Can anybody provide me with a code sample to write integration test for a controller which is secured with HTTPS?? With HTTP I am able to write, but with HTTPS I am getting certification error. Controller @RestController @RequestMapping("/rest/otm/v1") public class LoginController { @Autowired UserAuthenticationService userAuthService; @ExceptionHandler({ AuthenticationFailedException.class}) @RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseBody public void login

unit test Spring MissingServletRequestParameterException JSON response

会有一股神秘感。 提交于 2019-12-07 10:52:38
问题 I have POST method in a Spring boot rest controller as follows @RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map<String, String> bookmarkPost( @RequestParam(value="actionType",required=true) String actionType, @RequestParam(value="postId",required=true) String postId, @CurrentUser User user) throws Exception{ return service.bookmarkPost(postId, actionType, user); } now if I test with missing parameter in Postman I get an 400 http response and a

Testing Spring controllers using Powermock

强颜欢笑 提交于 2019-12-07 10:31:51
问题 I have a class which tests specific controller and it works fine @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml") @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class TestAdminController { //..... } I have used there Google's Mockito library from mockito.org to mock my underlying beans. Now my

Spring4 MVC Unit test does not compile

旧城冷巷雨未停 提交于 2019-12-06 19:10:50
问题 I have strange behaviour when trying to compile sources after Spring 3.2.5 → 4.0.0 version update. Faulty code snippet from ApplicationControllerTest.java (it is equivalent to code from documentation): import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; ... @Autowired private WebApplicationContext wac

Should Mockito be used with MockMvc's webAppContextSetup in Spring 4?

旧巷老猫 提交于 2019-12-06 12:43:39
问题 I'm having difficulties getting Mockito and MockMvc working together when I use the webAppContextSetup together. I'm curious if it's because I'm mixing the two in a way they were never intended. Source: https://github.com/zgardner/spring-boot-intro/blob/master/src/test/java/com/zgardner/springBootIntro/controller/PersonControllerTest.java Here is the test I'm running: package com.zgardner.springBootIntro.controller; import org.junit.Before; import org.junit.Test; import org.junit.runner

Spring Security Test and MockMvc supply null custom UserDetails parameter to REST Controller

点点圈 提交于 2019-12-06 10:51:41
问题 I'm trying to write an integration test that hits a REST endpoint and gets data for a specific authenticated user (the one I'm setting up in the test). I initially tried my setup with mockMvc = webAppContextSetup(wac).apply(springSecurity()).build() , but that was consistently failing with a BeanInstantiationException . With the help of Testing Spring Security and MvcMock using a custom UserDetails implementation, I was able to overcome that issue by switching up my setUp. Now that I've

unit test Spring MissingServletRequestParameterException JSON response

家住魔仙堡 提交于 2019-12-05 16:43:15
I have POST method in a Spring boot rest controller as follows @RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map<String, String> bookmarkPost( @RequestParam(value="actionType",required=true) String actionType, @RequestParam(value="postId",required=true) String postId, @CurrentUser User user) throws Exception{ return service.bookmarkPost(postId, actionType, user); } now if I test with missing parameter in Postman I get an 400 http response and a JSON body: { "timestamp": "2015-07-20", "status": 400, "error": "Bad Request", "exception": "org

Spring load testing

戏子无情 提交于 2019-12-05 12:44:15
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? 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 classpath and restart JMeter. Once done you'll be able to see your classes and methods in JUnit Request sampler

Spring4 MVC Unit test does not compile

爱⌒轻易说出口 提交于 2019-12-05 01:32:00
I have strange behaviour when trying to compile sources after Spring 3.2.5 → 4.0.0 version update. Faulty code snippet from ApplicationControllerTest.java (it is equivalent to code from documentation ): import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; ... @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup

Spring Security Test and MockMvc supply null custom UserDetails parameter to REST Controller

三世轮回 提交于 2019-12-04 17:51:31
I'm trying to write an integration test that hits a REST endpoint and gets data for a specific authenticated user (the one I'm setting up in the test). I initially tried my setup with mockMvc = webAppContextSetup(wac).apply(springSecurity()).build() , but that was consistently failing with a BeanInstantiationException . With the help of Testing Spring Security and MvcMock using a custom UserDetails implementation , I was able to overcome that issue by switching up my setUp. Now that I've switched my setup to use standaloneSetup , I'm able to call into my controller. But, no matter what I do, I