title: Mockito: How to mock WebClient configuring Beans
body: <p>I'm using Mockio, Wiremock and WebClient and I want to test my service layer.
  My goal is to use an instance of the webclient and do a real request to wiremock.</p>
  <p>Therefore I have to use a standard configuration and not my oauth config from the production mode.</p>
  <p>In the service class, I execute reuqets to another api. So the class under test ist annotated with @Service.</p>
  <p>Here is the class:</p>
  <pre><code>@Service
  public class UserServiceImpl implements UserService{
  
      private final Logger log = Logger.getLogger(this.getClass().getName());
      private final WebClient webClient;
      private final ApplicationConstants applicationConstants;
  
      public UserServiceImpl (WebClient webClient, ApplicationConstants applicationConstants) {
          this.applicationConstants = applicationConstants;
          this.webClient = webClient;
      }
  
      @Override
      public User getUserById(@NotNull(message = &quot;userId must not be null.&quot;) @NotBlank(message = &quot;userId must not be blank.&quot;) String userId) {
          return  webClient.get()...
  }
  </code></pre>
  <p>I configured my WebClient to use Oauth via two Bean Methods in a class annotated with @Configuration.</p>
  <pre><code>@Configuration
  public class WebClientConfig {
  
      @Bean
      public WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
         ...
      }
  
      /*
      Manages the auth process and token refresh process
       */
      @Bean
      public OAuth2AuthorizedClientManager authorizedClientManager(
              ClientRegistrationRepository clientRegistrationRepository,
              OAuth2AuthorizedClientRepository authorizedClientRepository) {
  
          ...
      }
  }
  </code></pre>
  <p>Because I want to use the webClient without oauth to call wiremock, I want to replace the Beans to return a simple Webclient.builder().build();</p>
  <p>So I did:</p>
  <pre><code>@ExtendWith({SpringExtension.class, WireMockExtension.class, MockitoExtension.class})
  public class TestClass {
  
      @Mock
      WebClientConfig webClientConfig;
  
      @MockBean
      WebClient webClient;
  
      @InjectMocks 
      UserServiceImpl userService;
  </code></pre>
  <p>In general as I understand Mockito, i would use my class under test ( the userServiceImpl) with <code>@InjectMocks</code>, so a real instance is used and the dependencies are injected. Therefor I have to provide a Mock for the Webclient. As I don't want to Mock the webclient and just want to configure it different, I do not have to use <code>@Mock</code>. Instead it should be somethig like <code>@MockBean</code> as this annotation creates a bean and replaces existing ones in the context. So I have to mock the Webclientconfig class with @Mock and define something like</p>
  <pre><code>when(webclientConfig).webclient(any(OAuth2AuthorizedClientManager.class)).thenReturn(Webclient.builder.build);
  </code></pre>
  <p>But this does not work as I always get a nullpointer exception on the call.
  So the basic questions are:</p>
  <ol>
  <li>Is my understanding of Mockito right?</li>
  <li>How do I have to Manage the Webclient config?</li>
  </ol>