title: Mockito + PowerMock - mock Thread.currentThread().getStackTrace()
body: <p>I'm currently trying to write a JUnit4 test for a package verification method.<br>
  The method in question simply checks if the call originates from the correct packages, to make sure it isn't called from outside. The method and the <a href="https://regex101.com/r/Dpmehr/1" rel="nofollow noreferrer">RegEx</a> itself works, however to be safe I'd like to add a unit-test.</p>
  
  <p>Simplified, this is the package test:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>package foo.bar.lib.util.unsafe;
  
  import java.util.regex.Pattern;
  
  public abstract class UnsafeUtil {
    private static final Pattern UNSAFE_PACKAGE_PATTERN =
        Pattern.compile("^foo\\.bar\\.lib(?:\\..*)?(?&lt;!unsafe)\\.\\w+$");
  
    protected static void testInternalUse() {
      final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
  
      for (int i = 2; i &lt; stack.length; ++i) {
        if (UNSAFE_PACKAGE_PATTERN.matcher(stack[i].getClassName()).find()) return;
      }
  
      throw new IllegalStateException("UnsafeUtil may not be used externally");
    }
  }
  </code></pre>
  
  <p>Now my test class looks like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>package foo.bar.lib.util.unsafe;
  
  import static org.junit.Assert.assertEquals;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.Mockito;
  import org.powermock.api.mockito.PowerMockito;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest(Thread.class)
  public class UnsafeUtilTest {
    private static final String expectedError = "UnsafeUtil may not be used externally";
  
    private static void setupStackTrace(String className) {
      final Thread fakeThread = Mockito.mock(Thread.class);
      final StackTraceElement[] fakeStack =
          new StackTraceElement[] {null, null, new StackTraceElement(className, "", "", 0)};
  
      PowerMockito.mockStatic(Thread.class);
      Mockito.when(Thread.currentThread()).thenReturn(fakeThread);
      Mockito.when(fakeThread.getStackTrace()).thenReturn(fakeStack);
    }
  
    @Test
    public void correctPackageTest() {
      setupStackTrace("foo.bar.lib.Main");
  
      try {
        CommonUnsafeUtil.testInternalUse();
      } catch (IllegalStateException e) {
        if (expectedError.equals(e.getMessage()))
          throw new AssertionError("An IllegalStateException should not have been thrown:", e);
      }
    }
  
    @Test
    public void incorrectPackageTest() {
      setupStackTrace(String.class.getName());
  
      try {
        CommonUnsafeUtil.testInternalUse();
      } catch (IllegalStateException e) {
        assertEquals(expectedError, e.getMessage());
      }
    }
  }
  </code></pre>
  
  <p><code>incorrectPackageTest</code> succeeds, however <code>correctPackageTest</code> fails with the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>java.lang.AssertionError: An IllegalStateException should not have been thrown:
      at foo.bar.lib.util.unsafe.UnsafeUtilTest.correctPackageTest(UnsafeUtilTest.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
      at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
      at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
      at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
      at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
      at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
      at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
      at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
      at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:116)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:59)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:39)
      at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
      at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
      at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
      at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
      at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
      at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
      at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
      at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.lang.IllegalStateException: UnsafeUtil may not be used externally
      at foo.bar.lib.util.unsafe.UnsafeUtil.testInternalUse(UnsafeUtil.java:17)
      at foo.bar.lib.util.unsafe.UnsafeUtilTest.correctPackageTest(UnsafeUtilTest.java:33)
      ... 56 more
  </code></pre>
  
  <p>Upon further inspection I found out that <code>Thread.currentThread().getStackTrace()</code> call returns the real stack trace for both calls instead of the the mocked one I created.</p>
  
  <p>What am I doing wrong?</p>