spring-test-mvc

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

二次信任 提交于 2019-12-04 15:23:28
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.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.beans.factory

How to test POST spring mvc

我的未来我决定 提交于 2019-12-04 13:53:15
问题 My problem is to how to call this. I could do MyObject o = new MyObject(); myController.save(o, "value"); but this is not what I would like to do. I would like the MyObject to be in the request post body? How can this be done? @Requestmapping(value="/save/{value}", method=RequestMethod.POST) public void post(@Valid MyObject o, @PathVariable String value{ objectService.save(o); } Just to be clear I am talking about unit testing. Edit: @RequestMapping(value = "/", method = RequestMethod.POST)

pass remoteUser value in HttpServletRequest to mockmvc perform test

前提是你 提交于 2019-12-04 13:01:05
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( "/course") .contentType(MediaType.APPLICATION_JSON) .andExpect( status().isOk() ) .andExpect( content()

How to mock Eureka when doing Integration Tests in Spring?

妖精的绣舞 提交于 2019-12-04 03:46:56
I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {FrontControllerApplication.class}) @WebAppConfiguration @ComponentScan @IntegrationTest({"server.port:0", "eureka.client.registerWithEureka:false", "eureka.client.fetchRegistry:false"}) @ActiveProfiles("integrationTest") public class MyControllerIT { In the application-integrationTest.properties I have the following Eureka Settings: ####### Eureka eureka.serviceUrl.default=http://localhost:8767/eureka/ eureka

Autowiring not working in Spring 3.1.2, JUnit 4.10.0

坚强是说给别人听的谎言 提交于 2019-12-04 00:55:44
问题 Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I'm having the problem that I can't get the annotation-based autowiring to work. Below are two samples, the one not using annotations, which is working fine. And the second one using annotation, which doesn't work, and I don't find the reason. I followed the samples of spring-mvc-test pretty much. Working : package com.company.web.api; // imports public class ApiTests { @Test public void testApiGetUserById() throws Exception {

Testing Spring asyncResult() and jsonPath() together

元气小坏坏 提交于 2019-12-03 11:15:11
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 { // Code goes here to locate relevant job and kick it off, waiting for result String message = <result

How to test POST spring mvc

扶醉桌前 提交于 2019-12-03 08:48:49
My problem is to how to call this. I could do MyObject o = new MyObject(); myController.save(o, "value"); but this is not what I would like to do. I would like the MyObject to be in the request post body? How can this be done? @Requestmapping(value="/save/{value}", method=RequestMethod.POST) public void post(@Valid MyObject o, @PathVariable String value{ objectService.save(o); } Just to be clear I am talking about unit testing. Edit: @RequestMapping(value = "/", method = RequestMethod.POST) public View postUser(ModelMap data, @Valid Profile profile, BindingResult bindingResult) { if

How to Mock the security context in Spring MVC for testing

倾然丶 夕夏残阳落幕 提交于 2019-12-03 07:04:53
问题 I need to test some protected urls, therefore I need to set up a mock security context in my tests (junit). In particular I need perform some gets and post against my web application, using an authenticated user. Below there is my code, I am able to create a such security context but I need to inject it in the 'MockMvc' object. I set the authentication object in the security context and it works, the output result of 'SecurityContextHolder.getContext().getAuthentication().getPrincipal()' is

How to Mock the security context in Spring MVC for testing

你说的曾经没有我的故事 提交于 2019-12-02 21:50:12
I need to test some protected urls, therefore I need to set up a mock security context in my tests (junit). In particular I need perform some gets and post against my web application, using an authenticated user. Below there is my code, I am able to create a such security context but I need to inject it in the 'MockMvc' object. I set the authentication object in the security context and it works, the output result of 'SecurityContextHolder.getContext().getAuthentication().getPrincipal()' is chanelle.evans@616747.com but when I call the GET on /profile I have an assertion error because I am

Issue with @WithUserDetails and spring boot 1.4 TestEntityManager

送分小仙女□ 提交于 2019-12-01 06:20:07
I have an issue with Spring Boot's TestEntityManager and @WithUserDetails annotation. Here is my test suite: public class AdvertisementAuthorizationTest extends AbstractIntegrationTest { private static final String IMPERSONATOR_EMAIL = "joe.hacker@gmail.com"; private final static String OWNER_OF_ADVERTISEMENT_EMAIL = "john.nice@gmail.com"; @Autowired private TestEntityManager testEntityManager; @Autowired private MockMvc mockMvc; private Advertisement advertisement; private UserAccount impersonator; private ObjectMapper mapper = new ObjectMapper(); @Before public void setUp() { advertisement =