Spring Testing - java.lang.IllegalArgumentException: Not enough variable values available to expand
I found out what I am doing wrong, using param() method is not the right way here as I have @PathVariable
and @RequestBody
in my Controller Methods as the parameters.
public Collection<UserEntity.UserAuthz> grantAuthz(@PathVariable("userId") String userId,
@RequestBody ArrayList<String> authorities) {
So I passed the @PathVariable
in the post()
method of the test.
MockMvcRequestBuilders.post(USER_URL,USER_ID)
As the required type is @RequestBody ArrayList<String>
instead of using the JSONObject
I used JSONArray
and used the content() method to send the JSONArray as the string.
Here are the changes I have made to the Test Method.
@Test
public void testGrantAuthorizationForUser() throws Exception{
Optional<UserEntity> userEntityAuthz = userRepository.findOneByUsername(USER_NAME);
Set<String> expectedAuthzList = (LinkedHashSet)userEntityAuthz.get().getAuthorizations();
List<String> grantList = new ArrayList<>();
grantList.add("ABC");
grantList.add("DEF");
grantList.add("GHI");
grantList.add("JKL");
grantList.add("MNO");
grantList.add("PQR");
grantList.add("STU");
grantList.add("VWX");
grantList.add("YZA");
JSONArray json = new JSONArray();
MvcResult grantAuthzResult = mockMvc.perform(MockMvcRequestBuilders.post(USER_URL,USER_ID)
.contentType(contentType)
.content(json.toString()))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
}