使用Mockito模拟功能会引发与定义不同的错误



我有几个模拟服务的测试,该测试调用相同的功能,并且在每个测试中都应丢下不同的错误以模拟不同的问题。我不明白为什么一个测试可以正常工作,并且抛出了例外,但是在相同的测试中,删除了NullPoInterException。

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, () -> 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, () -> userMutation.login(anyString(), anyString()));
    }
}
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.
    }
}

无效的代码

package com.band.api.exceptions;
public class InvalidInputException extends BaseGraphQLException {
    public InvalidInputException(String message) {
        super(message);
    }
}

完整的stacktrace

org.opentest4j.AssertionFailedError: Unexpected exception type thrown ==> expected: <com.band.api.exceptions.InvalidInputException> but was: <java.lang.NullPointerException>
    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

我不明白为什么一个测试会抛出正确的定义异常,而另一个测试会抛出NullPoInterException。如果他们俩都扔了无零件,那么我会有其他地方可以看,但是他们却没有。任何帮助将不胜感激。

问题与行:

assertThrows(InvalidInputException.class, () -> userMutation.login(anyString(), anyString()));

具体来说,您无法在此处致电anyString(),因为这是实际被调用的调用。您无法调用以anyString()为参数调用的函数,必须设置真实值。如果将行更改为assertThrows(InvalidInputException.class, () -> userMutation.login("", ""));,则应正常工作。

相关内容

  • 没有找到相关文章

最新更新