title: How to mock static access to spring-managed @Bean with Mockito?
body: <p>I have a <code>@Service</code> bean that I need <code>static</code> access to:</p>
  
  <pre><code>@Service
  public class MyBean implements InitializingBean {
      private static MyBean instance;
  
      @Override
      public void afterPropertiesSet() throws Exception {
          instance = this;
      }
  
      public static MyBean get() {
          return instance;
      }
  
      public String someMethod(String param) {
         return "some";
      }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>@Service
  public class OtherService {
      public static void makeUse() {
          MyBean myBean = MyBean.get();
      }
  }
  </code></pre>
  
  <p>Problem: when I write an integration <code>junit</code> test for <code>OtherService</code> that makes use of the stat <code>MyBean</code> access, the <code>instance</code> variable is always null.</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  public class ITest {
      @Autowired
      private OtherService service;
  
      @MockBean
      private MyBean myBean;
  
      @Before
      public void mock() {
          Mockito.when(myBean.someMethod(any()).thenReturn("testvalue");
      }
  
      @Test
      public void test() {
          service.makeUse(); //NullPointerException, as instance is null in MyBean
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong> how can I write an integration test when using such type of static access to a spring-managed bean?</p>