title: RestTemplateBuilder mocked with @MockBean is null
body: <p>I want to test a <code>Tasklet</code> implementation which uses an <code>@Autowired</code> <code>RestTemplateBuilder</code> to build a <code>RestTemplate</code>. The <code>RestTemplate</code> executes a request. I want to mock the response of this request.    </p>
  
  <pre><code>@ContextConfiguration(classes = DogTasklet.class )
  @RunWith(SpringRunner.class)
  public class DogTaskletTest {
  
      @MockBean
      RestTemplateBuilder restTemplateBuilder;
      private RestTemplate restTemplate = new RestTemplate();
  
      @Autowired
      private Tasklet sut;
  
      @Before
      public void setUp() throws Exception {
          given(this.restTemplateBuilder.build()).willReturn(restTemplate);
      }
  }
  </code></pre>
  
  <p>The <code>given()</code> statement throws a NPE because the <code>RestTemplateBuilder</code> instance is <code>null</code>. What have I missed?</p>
  
  <p>Update: I changed the test to the following which solves the NPE, now I have <code>null ResponseEntity</code> during <code>sut.execute()</code>.</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  public class DogTaskletTest {
  
  @TestConfiguration
  static class TestConfig {
  
      @Bean
      RestTemplateBuilder restTemplateBuilder() {
          RestTemplateBuilder restTemplateBuilder = mock(RestTemplateBuilder.class);
          RestTemplate restTemplate = mock(RestTemplate.class);
          ResponseEntity responseEntity = mock(ResponseEntity.class);
  
          given(restTemplateBuilder.build()).willReturn(restTemplate);
          given(restTemplate.execute(any(), any(), any(), any())).willReturn(responseEntity);
          given(responseEntity.getBody()).willReturn("{}");
  
          return restTemplateBuilder;
      }
  
      @Bean
      DogTasklet sut() {
          return new DogTasklet("string", restTemplateBuilder());
      }
  }
      @Test
      public void execute() throws Exception {
          // when
          sut.execute(stepContribution, chunkContext);
      }
  }
  </code></pre>