title: MVC Testing Spring Boot Mocking Keycloak Token
body: <p>I'm trying to do a MVC-Unittest for my with keycloak secured API.</p>
  <p>Using <a href="https://github.com/ch4mpy/spring-addons" rel="nofollow noreferrer">https://github.com/ch4mpy/spring-addons</a>, and according to the example the test should work just fine.</p>
  <p>test:</p>
  <pre class="lang-java prettyprint-override"><code>    @RunWith(SpringRunner.class)
  @WebMvcTest(controllers = StudentController.class)
  @AutoConfigureMockMvc
  @Import({ServletKeycloakAuthUnitTestingSupport.UnitTestConfig.class,
          KeycloakSecurityConfig.class})
  public class StudentControllerTests {
  
      @Autowired
      private MockMvc mockMvc;
  
      @MockBean
      private StudentService service;
  
      @MockBean
      private StudentRepository repository;
  
      //TODO: Fix keycloak bug
      @Test
      @WithMockKeycloakAuth(
              authorities = { &quot;USER&quot;, &quot;AUTHORIZED_PERSONNEL&quot; },
              id = @IdTokenClaims(sub = &quot;42&quot;),
              oidc = @OidcStandardClaims(
                      email = &quot;test@testing.com&quot;,
                      emailVerified = true,
                      nickName = &quot;TesterNickName&quot;,
                      preferredUsername = &quot;testName&quot;),
              accessToken = @KeycloakAccessToken(
                      realmAccess = @KeycloakAccess(roles = { &quot;TESTER&quot; }),
                      authorization = @KeycloakAuthorization(
                              permissions = @KeycloakPermission(rsid = &quot;toto&quot;, rsname = &quot;truc&quot;, scopes = &quot;abracadabra&quot;))),
              privateClaims = @ClaimSet(stringClaims = @StringClaim(name = &quot;foo&quot;, value = &quot;bar&quot;)))
      public void shouldTest() throws Exception {
          MvcResult result = this.mockMvc.perform(get(&quot;/students/test&quot;)).andReturn();
          assertEquals(&quot;abc&quot;, result.getResponse().getContentAsString());
      }
  }
  </code></pre>
  <p>application.yml</p>
  <pre><code>keycloak:
    realm: xxx
    auth-server-url: http://localhost:8081/auth
    ssl-required: external
    resource: client-interface
    use-resource-role-mappings: true
    credentials:
      secret: xxx
    bearer-only: true
    principal-attribute: preferred_username
  </code></pre>
  <p>I'm getting following stacktrace:</p>
  <pre><code>java.lang.NullPointerException
      at org.keycloak.adapters.KeycloakDeploymentBuilder.internalBuild(KeycloakDeploymentBuilder.java:57)
      at org.keycloak.adapters.KeycloakDeploymentBuilder.build(KeycloakDeploymentBuilder.java:202)
      at org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver.resolve(KeycloakSpringBootConfigResolver.java:39)
      at org.keycloak.adapters.springsecurity.config.KeycloakSpringConfigResolverWrapper.resolve(KeycloakSpringConfigResolverWrapper.java:40)
      at org.keycloak.adapters.AdapterDeploymentContext.resolveDeployment(AdapterDeploymentContext.java:89)
      at org.keycloak.adapters.springsecurity.filter.KeycloakPreAuthActionsFilter.doFilter(KeycloakPreAuthActionsFilter.java:82)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92)
      at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  [...]
  
  </code></pre>
  <p>Any idea how to fix?</p>
  <p>Greetings,
  rosario</p>