title: Spring Boot, testing controller with secret token, how to?
body: <p>In my microservice I have Spring boot controller which is receive also auth token from http request header and send it to another microservice for validation:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Autowired
  private AuthService authService;
  
  @GetMapping(path = "/find", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @ResponseBody
  @Timed
  @CrossOrigin
  public ResponseEntity&lt;?&gt; find(
          @RequestParam(name = "contactId", required = true) final Long contactId,
          @RequestHeader(name = "secret-token", required = true) String token
  ) {
  
      boolean checkTokenValidationFromAuthServer = authService.checkTokenValidationFromAuthServer(token);
  
      if (!checkTokenValidationFromAuthServer) {
          return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("token is invalid");
      }
  
      return ResponseEntity.status(HttpStatus.OK).body("Your contact here!");
  }
  </code></pre>
  
  <p>Auth Service must be like this</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  @Component
  public class AuthService {
  
      public boolean checkTokenValidationFromAuthServer(String token){
          //make rest call to auth microservice
          //validate token, return false, or true
  
          return false;
      }
  }
  </code></pre>
  
  <p>Basic test always will fail because I from test I do not know secret token, I want to mock or prevent controller from validation:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  public class PoolApplicationTests {
  
      @Autowired
      private PoolController controller;
  
      @Test
      public void controllerInitializedCorrectly() {
          assertThat(controller).isNotNull();
      }
  
      @Test
      public void testCall() {
  
          ResponseEntity&lt;?&gt; find = controller.find(1L, "token");
  
          assertThat(find.getStatusCode()).isEqualTo(200);
      }
  }
  </code></pre>
  
  <p>I do not know what is the best strategy for this kind of cases.</p>