So I don't think there is a lot of questions on here on Mockmvc from spring for testing springmvc controllers but I read through the spring tutorial https://spring.io/guides/tutorials/rest/2/ where there is a lot there but I was just trying to do a simple GET on a web service with a single id parameter. The controller looked like this:
@RestController
@RequestMapping("/demandrest")
public class DemandServicesController {
@Autowired
DemandService demandService;
@RequestMapping(value = "/demand/{$id}", method = RequestMethod.GET, headers= "Accept-application/jsson")
public Demand readDemand(@RequestParam(value="id", required=true) String id){
return demandService.readDemand(new Long(id));
}
and I wrote a unit test that uses the org.springframework.test.web.servlet.MockMvc and I attempt to mock out (really stub) the service call and do an assert on the status code and am getting status code 404. My test looks like this
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.web.servlet.MockMvc;
public class DemandServicesControllerTest {
MockMvc mockMvc;
@InjectMocks
DemandServicesController demandServicesController = new DemandServicesController();
@Mock
DemandService demandService;
@Before
public void setUpUnitTest() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = standaloneSetup(demandServicesController).
setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testReadDemandState() throws Exception {
Long id = new Long(99);
Demand demand = new Demand();
demand.setDescription("description");
when(demandService.readDemand(id)).thenReturn(demand );
this.mockMvc.perform(get("/demandrest/demand/{id}",id.toString()).
accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
I couple other things I'll mention before I hit post. So looking at the example from the tutorial I tried to just pick the stuff that I would only use so it isn't an exact copy. One big difference I had to do, and I suspect this has something to do with how in the pom the testing plugin is configured, I had to instantiate the controller where the example is smart enough to know to create an instance. Also, the example relies on Gradle for project set up and I just have a pom file with this project. Not sure if that makes a difference. Seems like this is new stuff but this is a pretty simple example. Thanks in advance for any help.
You should fix you RequestMapping from
@RequestMapping(value = "/demand/{$id}", method = RequestMethod.GET, headers= "Accept-application/jsson")
to
@RequestMapping(value = "/demand/{id}", method = RequestMethod.GET, headers= "Accept=application/json")
{$id}
should be just {id} and headers
have a typo and incorrect syntax. Perhaps you should consider using produces
and consumes
instead of the headers
param also.
This should fix the 404 status.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.