title: Spring Testing - java.lang.IllegalArgumentException: Not enough variable values available to expand
body: <p>I am writing Unit Tests for the below REST Controller which takes a UserID and grants a List of Authorities to that user.</p>
  
  <pre><code>    @RestController
      @RequestMapping("/user")
      @Api(value = "User", description = "User API")
      public class UserController{
  
      // some code
  
          @RequestMapping(method = RequestMethod.POST, value = "/{userId}/grantAuthz")
          @ApiOperation(value = "GrantAuthz", notes = "Grant Authorization")
          public Collection&lt;UserEntity.UserAuthz&gt; grantAuthz(@PathVariable("userId") String userId,
                                                             @RequestBody ArrayList&lt;String&gt; authorities) {
              UserEntity userEntity = userRepository.findOne(userId);
              if(userEntity == null) {
                  //TODO: throw and send resource not found
                  return null;
              }
              log.debug("Authorities to be granted to user " + userId + " are : " + authorities);
              for(String authz : authorities) {
                  log.debug("Adding Authorization " + authz);
                  userEntity.addUserAuthz(authz);
              }
              userRepository.save(userEntity);
              return userEntity.getAuthorities();
          }
  }
  </code></pre>
  
  <p>I wrote the below Unit Test for the UserController </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  @WebAppConfiguration
  public class UserControllerTest {
      private final Log log = LogFactory.getLog(getClass());
      private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
              MediaType.APPLICATION_JSON.getSubtype(),
              Charset.forName("utf8"));
  
      private MockMvc mockMvc;
      private HttpMessageConverter mappingJackson2HttpMessageConverter;
      private final String USER_URL = "/{userId}/grantAuthz";
      private final String USER_ID = "111";
      private final String USER_NAME = "MockUser";
  
      @Autowired
      private WebApplicationContext webApplicationContext;
      @Autowired
      private UserRepository userRepository;
  
      private String createdToken = null;
  
      @Autowired
      void setConverters(HttpMessageConverter&lt;?&gt;[] converters) {
          this.mappingJackson2HttpMessageConverter = Arrays.asList(converters).stream().filter(
                  hmc -&gt; hmc instanceof MappingJackson2HttpMessageConverter).findAny().get();
  
          Assert.assertNotNull("the JSON message converter must not be null",
                  this.mappingJackson2HttpMessageConverter);
      }
  
      @Before
      public void setup() throws Exception {
          this.mockMvc = webAppContextSetup(webApplicationContext).build();
      }
  
      @Test
      public void testGrantAuthorizationForUser() throws Exception{
          Optional&lt;UserEntity&gt; userEntityAuthz = userRepository.findOneByUsername(USER_NAME);
          Set&lt;String&gt; expectedAuthzList = (LinkedHashSet)userEntityAuthz.get().getAuthorizations();
  
          List&lt;String&gt; grantList = new ArrayList&lt;&gt;();
          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");
  
          JSONObject json = new JSONObject();
          json.put("grantList",grantList);
  
          MvcResult grantAuthzResult = mockMvc.perform(MockMvcRequestBuilders.post(USER_URL)
                  .contentType(contentType)
                  .param("userId",USER_ID)
                  .param("authorities",json.toString()))
                  .andExpect(status().isOk())
                  .andDo(print())
                  .andReturn();
      }
  }
  </code></pre>
  
  <p>When executed, my test is throwing an Illegal Argument Exception:</p>
  
  <blockquote>
    <p>"Not enough variable values available to expand 'userId'"</p>
  </blockquote>
  
  <p>I am sending the required URL Parameters using the .param() method in the test, what am I doing wrong ? I reffered this possible duplicate question but did not find it much useful. <a href="http://Using%20RestTemplate%20in%20Spring.%20Exception-%20Not%20enough%20variables%20available%20to%20expand" rel="noreferrer">Using RestTemplate in Spring. Exception- Not enough variables available to expand</a></p>