title: How to mock a global object in a class properly?
body: <p>Let say I have a class like so</p>
  <pre><code>Class A{
     private K Obj = (K)(AppContext.getSpringContext().getBean(&quot;obj&quot;))
     
    public void method1{
       // uses Obj
    }
  
    public void method2{
      // uses Obj
    }
  
  }
  </code></pre>
  <p>And I need to write junits for method1 and method2 by changing the behavior of Obj in method 1 and method 2. In my junit class I am setting the appcontext as so:</p>
  <pre><code>public class AccountInformationManagerTest {
  
      private CCBSAppContext appContext;
  
      @Mock
      ApplicationContext springContext;
       
      @Mock
      Object obj;
  
     @Before
      public void setup() throws Exception {
          appContext = new CCBSAppContext();
          appContext.setApplicationContext(springContext);
          Mockito.when(springContext.getBean(&quot;obj&quot;)).thenReturn(obj);
      }
  }
  </code></pre>
  <p>As you can see, I am setting globally the appcontext. I don't want mock the static calls as I am using jacoco and powermockito doesn't integrate very well with jacoco. The problem here is that appcontext is now a global object which is shared across all methods and I need to modify the behavior of obj as I test the two methods. This will create a concurrency issue. How can I resolve this?</p>