title: Mockito keeps calling the real method and not mock it
body: <p>I'm trying to create a test where I have to mock a method inside the class that I want to test. But it keeps calling the real method, but I want mock it.</p>
  
  <p>The method that I want to mock is </p>
  
  <blockquote>
    <p>extractSecretValue(String path)</p>
  </blockquote>
  
  <p>I know it's not mocking the method because there is a "println", and it's printing.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>I'm using JUnit 5</p>
  
  <p>The class that I want to test:</p>
  
  <pre><code>    @Configuration
      public class RestTemplateConfig {
  
          @Value("${******}")
          private String keystore;
  
          @Value("${******}")
          private String identificador;
  
          @Value("${******}")
          private String token;
  
          @Bean
          public RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyManagementException {
              SSLContext context = null;
  
              context = SSLContext.getInstance("TLSv1.2");
              context.init(null, null, null);
  
              List&lt;Header&gt; headers = new ArrayList&lt;&gt;();
              headers.add(new BasicHeader("Authorization", "Bearer " + extractSecretValue(token)));
  
              CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(context).setDefaultHeaders(headers)
                      .build();
  
              HttpComponentsClientHttpRequestFactory hcchr = new HttpComponentsClientHttpRequestFactory(httpClient);
  
              hcchr.setConnectionRequestTimeout(10000);
  
              return new RestTemplate(hcchr);
  
          }
  
          public String extractSecretValue(String path) {
  
              System.out.println("Test1");
              Path secretPath = Paths.get(path);
              String value = "";
              try (Stream&lt;String&gt; lines = Files.lines(secretPath)) {
                  value = lines.collect(Collectors.joining());
              } catch (IOException ignored) {
                  throw new ApplicationException(ignored);
              }
              return value.isEmpty() ? path : value;
          }
  
      }
  </code></pre>
  
  <p>The Test class:</p>
  
  <pre><code>    @ExtendWith(MockitoExtension.class)
      public class RestTemplateConfigTest {
  
          @Test
          public void return_restTemplateConfig() {
  
              RestTemplateConfig restTemplateConfig = new RestTemplateConfig();
  
              RestTemplateConfig restTemplateMock;
  
              RestTemplate restTemplate;
  
              restTemplateMock = Mockito.spy(restTemplateConfig);
  
              try {
                  when(restTemplateMock.extractSecretValue(anyString())).thenReturn("423424");
                  restTemplate = restTemplateMock.restTemplate();
              } catch (NoSuchAlgorithmException | KeyManagementException e) {
                  throw new ApplicationException(e);
              }
  
          }
  
      }
  </code></pre>
  
  <p>I've already tried this too:</p>
  
  <pre><code>doReturn("2332").when(restTemplateMock).extractSecretValue(anyString());
  </code></pre>