Integration Test: MockMvc is returning empty body for POST request - integration-testing

I do not know why the MvcResult returns an empty JSON for a GET request when the httpStatus is Ok. Moreover, through Postman I can tell that the data is indeed there and should be retrievable. I want to use the JSON data and map it to objects using the ObjectMapper's readValue() method. But in order to do that, I obviously need to be able to retrieve it first... help
The test does not fail, but when I try to System.out.println(json) - there is an empty line in the console where presumably the printed JSON should be. (I do not have the same issue when doing unit tests.)
`#SpringBootTest
#AutoConfigureMockMvc (addFilters = false) //in order to disable security: error 401
public class IntegrationTests {
#Autowired
private MockMvc mockMvc;
#Autowired
private ObjectMapper objectMapper;
#Test
public void test() {
String url = "/url";
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
String json = result.getResponse().getContentAsString();
System.out.println(json);
}`

Related

Spring Boot MVC Test 404 with Valid Request

I am using Spring Boot 2.0.6 and have set up a test for a controller: the method is as follows:
#Secured("ROLE_ADMIN")
#GetMapping(value = {"/maintainers/aircrafts/workorders/workitems/{wid}/parts"}, produces = "application/json")
#ResponseStatus(value = HttpStatus.OK)
Response<Page<WorkItem>> getPagedParts(
#PathVariable("wid") Optional<Long> workItemId,
#PageableDefault(page = DEFAULT_PAGE_NUMBER, size = DEFAULT_PAGE_SIZE)
#SortDefault.SortDefaults({
#SortDefault(sort = "partName", direction = Sort.Direction.ASC),
#SortDefault(sort = "partSpecification", direction = Sort.Direction.ASC)
}) Pageable pageable) {
LOG.info("looking for work: {}", workItemId);
return Response.of(workItemService.findAllPartsForWorkItem(workItemId.get(), pageable));
}
As you can see, it is supposed to do paging and sorting, but it doesn't even get past the path:
The test that tests it is as follows:
#ActiveProfiles("embedded")
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#EnableConfigurationProperties
#EnableJpaRepositories({ "au.com.avmaint.api" })
#AutoConfigureMockMvc
public class WorkItemControllerPartsFunctionalTest {
private static final Logger LOG = LoggerFactory.getLogger(WorkItemControllerFunctionalTest.class);
private String adminJwtToken;
#Autowired
private WebApplicationContext context;
#Autowired
private MockMvc mvc;
#Autowired
private UserService userService;
#Autowired
private RoleService roleService;
#Autowired
private CasaService casaService;
#Autowired
private MaintainerService maintainerService;
#Autowired
private MaintenanceContractService maintenanceContractService;
#Autowired
private WorkSetService workSetService;
#Autowired
private WorkSetTemplateService workSetTemplateService;
#Autowired
private AircraftService aircraftService;
Maintainer franks;
MaintenanceContract contract;
#Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
franks = MaintainerFixtures.createFranksMaintainer(maintainerService, maintenanceContractService, casaService);
adminJwtToken = UserAndRoleFixtures.adminToken(userService, roleService, franks);
contract = WorkItemFixtures.makeDetailedJobOnContract(franks, maintainerService, maintenanceContractService, workSetTemplateService, casaService, aircraftService);
}
#Test
public void findingWorkItemsWithoutParts() throws Exception {
Set<WorkSet> sets = contract.getWorkOrders().stream().findFirst().get().getWorkSets();
WorkSet hundredHourly = sets.stream().filter(s -> s.getName().equals("100 Hourly for PA-31")).findFirst().orElse(null);
WorkItem opening = hundredHourly.getWorkItems().stream().filter(wi -> wi.getTitle().equals("Opening the aircraft")).findFirst().orElse(null);
LOG.info("opening item: {}", opening);
LOG.info("HUNDRED: {}", hundredHourly);
mvc.perform(get("/maintainers/aircrafts/workorders/workitems/" + opening.getId() + "/parts")
.header(AUTHORIZATION_HEADER, "Bearer " + adminJwtToken))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.payload").isNotEmpty())
.andExpect(jsonPath("$.payload.content").isNotEmpty())
.andExpect(jsonPath("$.payload.pageable").isNotEmpty())
.andExpect(jsonPath("$.payload.last").value(false))
.andExpect(jsonPath("$.payload.totalPages").value(3)) // page count
.andExpect(jsonPath("$.payload.totalElements").value(9)) // total count
.andExpect(jsonPath("$.payload.size").value(4)) // elements per page
.andExpect(jsonPath("$.payload.numberOfElements").value(4)) // elements in page
.andExpect(jsonPath("$.payload.number").value(0)) // current page number
.andExpect(jsonPath("$.payload.content").isArray())
// oops, lets not check dates, they're created on the instant
.andExpect(jsonPath("$.payload.content[0].pos").value("1"))
.andExpect(jsonPath("$.payload.content[0].title").value("Opening the aircraft"))
.andExpect(jsonPath("$.payload.content[0].category").value("AIRFRAME"))
;
}
#After
public void tearDown() {
MaintainerFixtures.removeFranks(franks, maintainerService, aircraftService);
WorkItemFixtures.killJobs(workSetService, workSetTemplateService);
UserAndRoleFixtures.killAllUsers(userService, roleService);
}
}
As the project makes extensive use of JPA, there are annotations and a lot of data setup, but all of this has worked fine with other tests and there don't appear to be any problems with the data. In fact a peek at the JSON output for the work order that this method should be querying...
work order JSON
Basically has all the data correctly set up. The spring boot startup includes this line:
2018-11-12 06:32:17.362 INFO 83372 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/maintainers/aircrafts/workorders/workitems/{wid}/parts],methods=[GET],produces=[application/json]}" onto au.com.avmaint.api.common.Response<org.springframework.data.domain.Page<au.com.avmaint.api.aircraft.model.WorkItem>> au.com.avmaint.api.aircraft.WorkItemController.getPagedParts(java.util.Optional<java.lang.Long>,org.springframework.data.domain.Pageable)
So the path appears to be OK
and now to the .andDo(print()) output:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /maintainers/aircrafts/workorders/workitems/5/parts
Parameters = {}
Headers = {Authorization=[Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJmcmFua0BmcmFua3MuY29tIiwic2NvcGVzIjpbIlJPTEVfQURNSU4iLCJST0xFX0JBU0lDIl0sImV4cCI6MTU0MjgyODczOH0.QOTiyWG_pVL9qb8MDG-2c_nkTnsIzceUH-5vvtmpZhBcdro9HqVADojK0-c6B1sAOOYOcprpwg4-wrBF0PGweg]}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
and the 404. So I guess I'm breaking something somewhere, I just can't see what it is, can anyone help with this?
Sorry everyone, the effect of tearing my hair out for ages, finally posting the question and then finding the problem moments later.
The issue was that I forgot to put /api as the prefix on the path in the test. This prefix is put on the top of every controller with:
#RestController
#RequestMapping("/api")
public class WorkItemController {
so, yeah: it works now

Issue testing Accept request header in Spring MockMvc

I am trying to unit test SpringBoot Controller. Objective is to test that we return 406 Not Acceptable when request header contains Accept as anything other than application/json. But I find its not working with MockMvc. I am getting 200 OK instead of 406. Any ideas please! Of course, service returns 406 as expected when I test using Postman or any rest client.
#Test
public void shouldRejectIfInvalidAcceptHeaderIsPassed() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept","application/xml");
httpHeaders.add("Authorization", "some jwt token");
this.mockMvc.perform(post("/sample/test")
.contentType(MediaType.APPLICATION_JSON)
.headers(httpHeaders)
.content(toJson("")))
.andExpect(status().isNotAcceptable());
// Then
verify(mockSampleService).getSampleOutput();
}
And my controller looks like,
#RestController
#Validated
#RequestMapping(PATH_PREFIX)
public class SampleController {
public static final String PATH_PREFIX = “/sample”;
private final SampleService sampleService;
#Autowired
public SampleController(SampleService sampleService) {
sampleService = sampleService;
}
#RequestMapping(value = “/test”, method = RequestMethod.POST))
public SampleResponse createSession() {
return sampleService.getSampleOutput();
}
}
You might consider using the accept() method instead, e.g.
mvc.perform( MockMvcRequestBuilders
.get("/some/test/url")
.content(...)
.accept(MediaType.APPLICATION_XML)
.andExpect(...)
Ahh! I have identified and fixed the issue. In the controller we need to have, #RequestMapping(headers="Accept=application/json"). Else MockMvc is not able to identify the expected format.

SpringWebMvcTest - Test Requestbody using #Valid and custom validation

I am trying to test my controller endpoint and my requestbody annotated with #Valid annotation. My Testclass looks like the follow:
#RunWith(SpringRunner.class)
#WebMvcTest(value = BalanceInquiryController.class, secure = false)
public class BalanceInquiryControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private BalanceInquiryController balanceInquiryController;
#Test
public void testGetBalanceInquiry() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/com/balanceInquiry")
.accept(MediaType.APPLICATION_JSON)
.content("{\"comGiftCard\":{\"cardNumber\":\"1234567890\",\"pinNumber\":\"0123\"},\"comMerchant\":\"MERCHANT1\"}")
.contentType(MediaType.APPLICATION_JSON);
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
assertEquals(HttpStatus.OK.value(), response.getStatus());
}
}
My Controller - #PostMapping looks like that:
#PostMapping(value = "/com/balanceInquiry")
public ResponseEntity<?> getBalanceInquiry(#Valid #RequestBody BalanceInquiryModel balanceInquiry, Errors errors) {
if (errors.hasErrors()) {
return new ResponseEntity<String>("Validation error", HttpStatus.BAD_REQUEST);
}
//do any stuff...
return new ResponseEntity<BalanceInquiryResponse>(balanceInquiryResponse, HttpStatus.OK);
}
My BalanceInquiryModel is annotated with #Valid and has some hibernate and custom validations behind. Those validations are all ok and already unit tested.
What I like to test is my endpoint where I send a valid json request body expecting a 200 response and also an invalid json request body expecting a 400 response validated by the set #Valid implementation.
For example an unvalid call is to send no pinNumber or length < 4.
I have read some threads and some uses MockMvcBuilders.standaloneSetup() to mock the full controller. But I wont do a full integration test.
Not quite sure how to go on with this situation and if I should go on.
P.S.: At the moment I get always a 200 response no matter if the validation should give an error or not.
Here a gist for more code and the validation classes/models.
Here's one of my example I work on my project
hope it help you out:
I have a global exception handler to handler my MethodArgumentNotValidException and throw it
#RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> createUser(#Valid #RequestBody User user) {
User savedUser = userService.save(user);
return new ResponseEntity<User>(savedUser, HttpStatus.CREATED);
}
public void testAdduser() throws Exception{
final User request = new User();
request.setFirstName("Test");
request.setLastName("some description");
mockMvc.perform(post(END_POINT+"/add")
.contentType(MediaType.APPLICATION_JSON)
.content(stringify(request))
).andDo(print()).andExpect(status().isUnprocessableEntity())
;
}
private String stringify(Object object) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(object);
}
Update:
I think your main problem is that you are using #WebMvcTest in stead of #SpringBootTest.
the different between 2 of them is that:
#SpringBootTest annotation will loads complete application and injects all the beans which is can be slow.
#WebMvcTest - for testing the controller layer. it doesn't inject other bean beside the #RestController
so if you are just testing just pure controller to see u can reach the endpont then you can just use #WebMvcTest which will make your test run faster.
but in your case, you want it to run the spring validation, you will need to use #SpringBootTest
for detailed: https://spring.io/guides/gs/testing-web/

Spring boot application fails on mockmvc test

I have a simple controller test that looks like this
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = CuponzaApiApplication.class)
#WebAppConfiguration
public class UserControllerTest {
private MockMvc mockMvc;
#Autowired
protected WebApplicationContext wac;
#Autowired
UserRepository userRepository;
#Before
public void setUp(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
#Test
public void createUser() throws Exception{
CuponzaUser user = new CuponzaUser("some#test.com", "firstName", "lastName");
ObjectWriter jackson = new ObjectMapper().writer().withDefaultPrettyPrinter();
mockMvc.perform(post("/user/add").content(jackson.writeValueAsString(user)).contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"));
}
however it fails saying the following
java.lang.AssertionError: Content type not set
here is my controller
#RestController
public class UserController {
#Autowired
UserRepository userRepository;
#RequestMapping(value = "/user/add",method = RequestMethod.POST,produces={MediaType.APPLICATION_JSON_VALUE})
public void AddUser(#RequestBody CuponzaUser user, HttpServletResponse response){
if(user ==null){
response.setStatus(HttpStatus.BAD_REQUEST.value());
return;
}else{
user.setCreationDate(new Date());
user.setLastSeenDate(new Date());
userRepository.save(user);
//response.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
return;
}
}
I dont want to manually add the content type header for each response , and i thought that the "produces" annotation should take care of this
any ideas?
This tends to be a little confusing - produces parameter of a #RequestMapping annotation does not really modify the response header, it is a way to narrow down the appropriate handler method based on the Accept header that the user has specified. Think of it this way, #RequestMapping and all the parameters associated with it is just a way to filter down to the appropriate method for Spring MVC to call.
The MessageConverter responsible for converting the responses to the appropriate media type does plug in the response Content-Type header, I think the issue in your case is because you are not setting the Accept header in your mock test - .accept(MediaType.APPLICATION_JSON)
The issue is that you're not returning anything. Your response body is empty.
In a way it makes sense, there is no content, what would be the point in defining a Content-Type? Setting the Accept header also won't get you anywhere. Furthermore, you should be able to reproduce this same behaviour outside of your unit tests too, i.e. it's not an issue with your unit test/mock setup.
You could either:
return some content
consider returning a 204 (No Content), if you really don't want to return anything (still wouldn't give you a Content-Type header but it would make clear that there is no content)
add the header manually as in the workaround commented out in your question

Testing Spring's #RequestBody using Spring MockMVC

I am trying to test a method that posts an object to the database using Spring's MockMVC framework. I've constructed the test as follows:
#Test
public void testInsertObject() throws Exception {
String url = BASE_URL + "/object";
ObjectBean anObject = new ObjectBean();
anObject.setObjectId("33");
anObject.setUserId("4268321");
//... more
Gson gson = new Gson();
String json = gson.toJson(anObject);
MvcResult result = this.mockMvc.perform(
post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andReturn();
}
The method I'm testing uses Spring's #RequestBody to receive the ObjectBean, but the test always returns a 400 error.
#ResponseBody
#RequestMapping( consumes="application/json",
produces="application/json",
method=RequestMethod.POST,
value="/object")
public ObjectResponse insertObject(#RequestBody ObjectBean bean){
this.photonetService.insertObject(bean);
ObjectResponse response = new ObjectResponse();
response.setObject(bean);
return response;
}
The json created by gson in the test:
{
"objectId":"33",
"userId":"4268321",
//... many more
}
The ObjectBean class
public class ObjectBean {
private String objectId;
private String userId;
//... many more
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
//... many more
}
So my question is: how to I test this method using Spring MockMVC?
Use this one
public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
#Test
public void testInsertObject() throws Exception {
String url = BASE_URL + "/object";
ObjectBean anObject = new ObjectBean();
anObject.setObjectId("33");
anObject.setUserId("4268321");
//... more
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String requestJson=ow.writeValueAsString(anObject );
mockMvc.perform(post(url).contentType(APPLICATION_JSON_UTF8)
.content(requestJson))
.andExpect(status().isOk());
}
As described in the comments, this works because the object is converted to json and passed as the request body. Additionally, the contentType is defined as Json (APPLICATION_JSON_UTF8).
More info on the HTTP request body structure
the following works for me,
mockMvc.perform(
MockMvcRequestBuilders.post("/api/test/url")
.contentType(MediaType.APPLICATION_JSON)
.content(asJsonString(createItemForm)))
.andExpect(status().isCreated());
public static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
The issue is that you are serializing your bean with a custom Gson object while the application is attempting to deserialize your JSON with a Jackson ObjectMapper (within MappingJackson2HttpMessageConverter).
If you open up your server logs, you should see something like
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2013-34-10-10:34:31': not a valid representation (error: Failed to parse Date value '2013-34-10-10:34:31': Can not parse date "2013-34-10-10:34:31": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: java.io.StringReader#baea1ed; line: 1, column: 20] (through reference chain: com.spring.Bean["publicationDate"])
among other stack traces.
One solution is to set your Gson date format to one of the above (in the stacktrace).
The alternative is to register your own MappingJackson2HttpMessageConverter by configuring your own ObjectMapper to have the same date format as your Gson.
I have encountered a similar problem with a more recent version of Spring. I tried to use a new ObjectMapper().writeValueAsString(...) but it would not work in my case.
I actually had a String in a JSON format, but I feel like it is literally transforming the toString() method of every field into JSON. In my case, a date LocalDate field would end up as:
"date":{"year":2021,"month":"JANUARY","monthValue":1,"dayOfMonth":1,"chronology":{"id":"ISO","calendarType":"iso8601"},"dayOfWeek":"FRIDAY","leapYear":false,"dayOfYear":1,"era":"CE"}
which is not the best date format to send in a request ...
In the end, the simplest solution in my case is to use the Spring ObjectMapper. Its behaviour is better since it uses Jackson to build your JSON with complex types.
#Autowired
private ObjectMapper objectMapper;
and I simply used it in my test:
mockMvc.perform(post("/api/")
.content(objectMapper.writeValueAsString(...))
.contentType(MediaType.APPLICATION_JSON)
);

Resources