title: Cannot mock Security manager using powermokito
body: <p>By looking at the answer written by <code>Lauri</code> in <a href="https://stackoverflow.com/questions/9347354/mockito-mock-of-securitymanager-throwing-an-exception">Mockito mock of SecurityManager throwing an exception</a> I wrote a unit test by mocking the Security manager. Below is the test case</p>
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest(System.class)
  public class TestClass {
      @Test
      public void testcheckSecurity() {
          //mocking the System class
          PowerMockito.mockStatic(System.class);
          SecurityManager secMan = PowerMockito.mock(SecurityManager.class);
          PowerMockito.when(System.getSecurityManager()).thenReturn(secMan);
          List&lt;String&gt; allowedClasses = Arrays.asList(&quot;ClassA&quot;, &quot;ClassB&quot;, &quot;ClassC&quot;, &quot;ClassD&quot;);
          BaseUtils.checkSecurity(allowedClasses);
  
      }
  }
  </code></pre>
  <p>and this is testing the static method below</p>
  <pre><code>public class BaseUtils{    
  public static void checkSecurity(List&lt;String&gt; allowedClasses) {
          SecurityManager secMan = System.getSecurityManager();
          if (secMan != null) {
              StackTraceElement[] trace = Thread.currentThread().getStackTrace();
              String callingClass = trace[3].getClassName();
              if (!allowedClasses.contains(callingClass)) {
                  secMan.checkPermission(new ManagementPermission(&quot;control&quot;));
              }
          }
      }
  }
  </code></pre>
  <p>But when I debug the test case the SecurityManager <code>secMan</code> is null in <code>checkSecurity(List&lt;String&gt; allowedClasses)</code> method.</p>
  <p>What I am doing wrong? Please help me to fix this.</p>
  <p>Thanks in advance</p>