title: Springboot & Mockito - Using ReflectionTestUtils.setField calling method twice
body: <p>I want to inject value to a private field annotated with @Value in my Junit5 unit test.</p>
  <pre><code>@Value(&quot;$(invoice.maxRetry)&quot;)
  private maxRetry;
  </code></pre>
  <p>I referred <a href="https://stackoverflow.com/questions/23162777/how-do-i-mock-an-autowired-value-field-in-spring-with-mockito">this</a> and used ReflectionTestUtils.setField which solved my problem by injecting a value but failed when verifying the no. of times the method get called.</p>
  <p>MyClass:</p>
  <pre><code>    public class MessageProcessor {
           
      @Value(&quot;$(invoice.maxRetry)&quot;)
          private maxRetry;
      
      protected void handleMessage() {
              if(retry &gt; maxRetry) {
                  kafkaTemplate.sendMessage(msg);
         }
  }
  </code></pre>
  <p>TestClass:</p>
  <pre><code>@ExtendWith(MockitoExtension.class)
  public class MessageProcessorTest {
  
  @Mock
  private kafkaTemplate kafkaTemplate;
  
  @Mock
  private MessageProcessor messageProcessor
  
  @Test
  test() {
       ReflectionTestUtils.setField(messageProcessor, &quot;maxRetry&quot;, &quot;5&quot;);
       doNothing().when(kafkaTemplate).sendMessage(any());
       messageProcessor.handleMessage();
       verify(kafkaTemplate).sendMessage(any());
    }
  }
  </code></pre>
  <p>Error running above test</p>
  <pre><code>org.mockito.exceptions.verification.TooManyActualInvocations: 
  kafkaTemplate.sendMessage(&lt;any&gt;);
  Wanted 1 time:
  But was 2
  </code></pre>
  <p>I want <em>kafkaTemplate.sendMessage();</em> to be called only once but getting called twice after adding ReflectionTestUtils.</p>
  <p>Need advice on how to fix this.</p>