title: Mocking a function with Mockito throws different error than defined
body: <p>I have several tests with a mocked service which call the same function and in each test should throw a different error to simulate a different problem. I don't understand why one test works just fine and the exception is thrown, but in an identical test a NullPointerException is thrown.</p>
  
  <pre><code>class UserMutationTest {
      private UserService mockUserService;
      private UserMutation userMutation;
  
      @BeforeEach
      void setup() {
          mockUserService = Mockito.mock(UserService.class);
          userMutation = new UserMutation(mockUserService);
      }
  
      //This test works perfectly fine, DataAccessresourceFailureException is thrown and caugh
      //and then a BaseGraphQLException is thrown which is exactly what's expected
      @Test
      void loginDatabaseUnavailable(){
          when(mockUserService.login(anyString(), anyString())).thenThrow(DataAccessResourceFailureException.class);
          assertThrows(BaseGraphQLException.class, () -&gt; userMutation.login(anyString(), anyString()));
      }
  
      //This test which is a copy/paste of the above test except for instead it's supposed
      //to throw InvalidInputException, but when called it actually throws NullPointerException.
      @Test
      void loginInvalidCredentials(){
          when(mockUserService.login(anyString(), anyString())).thenThrow(InvalidInputException.class);
          assertThrows(InvalidInputException.class, () -&gt; userMutation.login(anyString(), anyString()));
      }
  }
  </code></pre>
  
  <pre><code>public class UserMutation implements GraphQLMutationResolver {
      private final UserService userService;
  
      @Autowired
      public UserMutation(UserService userService) {
          this.userService = userService;
      }
  
      public User login(String username, String password){
          try {
              User u = userService.login(username, password);
              return u;
          } catch (DataAccessResourceFailureException e) {
              //Database Unavailable
              log.debug(e.toString());
              BaseGraphQLException ex = new BaseGraphQLException("Database Unavailable");
              ex.addExtension("Database", "Unavailable");
              throw ex;
          } catch (InvalidInputException e) {
              throw e;
          }
          //If I add a catch for NullPointException here it is actually caught, which makes no sense to me.
      }
  }
  </code></pre>
  
  <p>Code for InvalidInputException </p>
  
  <pre><code>package com.band.api.exceptions;
  
  public class InvalidInputException extends BaseGraphQLException {
      public InvalidInputException(String message) {
          super(message);
      }
  }
  </code></pre>
  
  <p>Full stacktrace</p>
  
  <pre><code>org.opentest4j.AssertionFailedError: Unexpected exception type thrown ==&gt; expected: &lt;com.band.api.exceptions.InvalidInputException&gt; but was: &lt;java.lang.NullPointerException&gt;
  
      at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:65)
      at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:37)
      at org.junit.jupiter.api.Assertions.assertThrows(Assertions.java:2920)
      at com.band.api.UserMutationTest.loginInvalidCredentials(UserMutationTest.java:87)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:108)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
      at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
      at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
      at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: java.lang.NullPointerException
      at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.filter(StackTraceFilter.java:32)
      at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.filter(ConditionalStackTraceFilter.java:23)
      at org.mockito.internal.stubbing.answers.ThrowsException.answer(ThrowsException.java:49)
      at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:39)
      at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:96)
      at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
      at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:35)
      at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:63)
      at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:49)
      at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(MockMethodInterceptor.java:110)
      at com.band.api.services.UserService$MockitoMock$583046591.login(Unknown Source)
      at com.band.api.resolvers.UserMutation.login(UserMutation.java:51)
      at com.band.api.UserMutationTest.lambda$loginInvalidCredentials$3(UserMutationTest.java:87)
      at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:55)
      ... 42 more
  
  
  Process finished with exit code -1
  </code></pre>
  
  <p>I don't understand why one test would throw the correct defined exception, but another would throw a NullPointerException. If they both threw NullPointer then I would have somewhere else to look, but they don't.
  Any help would be greatly appreciated.</p>