Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用自定义参数匹配器第二次调用Mockito.when时的NPE_Java_Unit Testing_Mockito_Junit4 - Fatal编程技术网

Java 使用自定义参数匹配器第二次调用Mockito.when时的NPE

Java 使用自定义参数匹配器第二次调用Mockito.when时的NPE,java,unit-testing,mockito,junit4,Java,Unit Testing,Mockito,Junit4,你好 假设我有一个接口。接口有一个方法,该方法接受某些类的一个或两个实例。在单元测试类中,我模拟这个接口,并希望用如下逻辑模拟它: 如果参数与template1匹配,则返回值1 如果参数与template2匹配,则返回值2 当我只模拟第一个案例时,测试运行平稳。当我添加第二种情况(与第一种情况一起)时,测试会与NPE一起崩溃,如: java.lang.NullPointerException at org.some.pkg.TestTest$ArgMatcher.matches(TestTest

你好

假设我有一个接口。接口有一个方法,该方法接受某些类的一个或两个实例。在单元测试类中,我模拟这个接口,并希望用如下逻辑模拟它:

  • 如果参数与template1匹配,则返回值1
  • 如果参数与template2匹配,则返回值2
  • 当我只模拟第一个案例时,测试运行平稳。当我添加第二种情况(与第一种情况一起)时,测试会与NPE一起崩溃,如:

    java.lang.NullPointerException
    at org.some.pkg.TestTest$ArgMatcher.matches(TestTest.java:54)
    at org.some.pkg.TestTest$ArgMatcher.matches(TestTest.java:43)
    at org.mockito.internal.invocation.TypeSafeMatching.apply(TypeSafeMatching.java:24)
    at org.mockito.internal.invocation.MatcherApplicationStrategy.forEachMatcherAndArgument(MatcherApplicationStrategy.java:83)
    at org.mockito.internal.invocation.InvocationMatcher.argumentsMatch(InvocationMatcher.java:152)
    at org.mockito.internal.invocation.InvocationMatcher.matches(InvocationMatcher.java:81)
    at org.mockito.internal.stubbing.InvocationContainerImpl.findAnswerFor(InvocationContainerImpl.java:91)
    at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:87)
    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.interceptAbstract(MockMethodInterceptor.java:128)
    at org.some.pkg.Interface$MockitoMock$1549778145.callMe(Unknown Source)
    at org.some.pkg.TestTest.test(TestTest.java:29)
    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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    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)
    
    [MockitoHint] TestTest.test (see javadoc for MockitoHint):
    [MockitoHint] 1. Unused... -> at org.some.pkg.TestTest.test(TestTest.java:24)
    [MockitoHint]  ...args ok? -> at org.some.pkg.TestTest.test(TestTest.java:29)
    
    
    Process finished with exit code 255
    
    下面是一个设置示例

    文件:Interface.java——模拟的接口

    package org.some.pkg;
    公共接口{
    公共类Arg{
    私有字符串str;
    私人INTV;
    公共参数(字符串str,int v){
    this.str=str;
    这个,v=v;
    }
    公共字符串getStr(){
    返回str;
    }
    公共int getV(){
    返回v;
    }
    }
    int callMe(Arg a1,Arg a2);
    }
    
    文件:TestedClass.java——一些委托类(实际上是经过测试的一个)

    package org.some.pkg;
    导入org.springframework.beans.factory.annotation.Autowired;
    公共类测试类{
    @自动连线
    接口代表;
    int callMe(Interface.Arg a1,Interface.Arg a2){
    INTX=委托调用(a1,a2);
    返回x*2;
    }
    }
    
    文件:TestTest.java——测试

    package org.some.pkg;
    导入org.junit.Assert;
    导入org.junit.Test;
    导入org.junit.runner.RunWith;
    导入org.mockito.ArgumentMatcher;
    导入org.mockito.InjectMocks;
    导入org.mockito.Mock;
    导入org.mockito.junit.MockitoJUnitRunner;
    导入静态org.mockito.ArgumentMatchers.argThat;
    导入静态org.mockito.mockito.when;
    @RunWith(MockitoJUnitRunner.class)
    公共类测试{
    @嘲弄
    专用接口iface;
    @注射模拟
    私有TestedClass TestedClass;
    @试验
    公开无效测试(){
    //当-1
    什么时候打电话给我(
    argThat(新的ArgMatcher(“start1”,10)),
    argThat(新的ArgMatcher(“start2”,5))
    )).然后返回(10);
    //何时-2
    什么时候打电话给我(
    argThat(新的ArgMatcher(“start3”,5)),
    argThat(新的ArgMatcher(“start4”,2))
    )).然后返回(3);
    //试验
    int v=testedClass.callMe(
    新的Interface.Arg(“start1后缀”,3),
    Arg(“start2,后缀”,4));
    System.out.println(“V=”+V);
    Assert.assertEquals(20,v);
    }
    类ArgMatcher实现ArgumentMatcher{
    私有最终字符串开始;
    私有最终整数阈值;
    公共ArgMatcher(字符串开始,整数阈值){
    this.start=start;
    这个阈值=阈值;
    }
    @凌驾
    公共布尔匹配(Interface.Arg参数){
    返回argument.getV()
    如果我在when-2块中注释掉
    ,则测试运行良好且平稳。否则,我看到一个NPE没有任何明显的原因

    Mockito的版本是2.24.0 JUnit是版本4


    更新:第二次调用
    Mockito的斗争。当
    知道在一个单元测试中可以多次调用mock方法时。

    好吧,我找到了一个解决方法,使用
    when(mock.callMe(any(Arg.class)、any(Arg.class))。然后是Answer(新答案{…})

    好吧,我已经找到了一个解决方法,使用
    when(mock.callMe(any(Arg.class)、any(Arg.class)).thenAnswer(new-Answer{…})

    面临类似的问题,解决方案是按照@Morfic提出的方法进行重写(假设Mockito是静态导入的):

    与:

    然而,我不知道或解释为什么第一个失败,第二个工作正常

    JUnit 4测试用例以显示故障和工作选项:

    class MyBean{
        public Integer id;
        public String name;
    
        public MyBean(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
    
    interface MyService{
        Integer measure(MyBean myBean);
    }
    
        @Test
        public void testThatFailsWithNPE(){
    
            when(myService.measure(argThat((mb) -> Objects.equals(mb.id, 123) && Objects.equals(mb.name, "123")))).thenReturn(123);
            when(myService.measure(argThat((mb) -> Objects.equals(mb.id, 321) && Objects.equals(mb.name, "321")))).thenReturn(321);
    
            assertEquals(Integer.valueOf(123), myService.measure(new MyBean(123, "123")));
            assertEquals(Integer.valueOf(321), myService.measure(new MyBean(321, "321")));
            assertEquals(Integer.valueOf(0), myService.measure(new MyBean(777, "777")));
        }
    
        @Test
        public void testThatPasses(){
    
            doReturn(123).when(myService).measure(argThat((mb) -> Objects.equals(mb.id, 123) && Objects.equals(mb.name, "123")));
            doReturn(321).when(myService).measure(argThat((mb) -> Objects.equals(mb.id, 321) && Objects.equals(mb.name, "321")));
    
            assertEquals(Integer.valueOf(123), myService.measure(new MyBean(123, "123")));
            assertEquals(Integer.valueOf(321), myService.measure(new MyBean(321, "321")));
            assertEquals(Integer.valueOf(0), myService.measure(new MyBean(777, "777")));
        }
    

    对于Mockito 2.28.2和3.1.0:

    面临类似的问题,解决方案是按照@Morfic提出的方法进行重写(假设Mockito是静态导入的):

    与:

    然而,我不知道或解释为什么第一个失败,第二个工作正常

    JUnit 4测试用例以显示故障和工作选项:

    class MyBean{
        public Integer id;
        public String name;
    
        public MyBean(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
    
    interface MyService{
        Integer measure(MyBean myBean);
    }
    
        @Test
        public void testThatFailsWithNPE(){
    
            when(myService.measure(argThat((mb) -> Objects.equals(mb.id, 123) && Objects.equals(mb.name, "123")))).thenReturn(123);
            when(myService.measure(argThat((mb) -> Objects.equals(mb.id, 321) && Objects.equals(mb.name, "321")))).thenReturn(321);
    
            assertEquals(Integer.valueOf(123), myService.measure(new MyBean(123, "123")));
            assertEquals(Integer.valueOf(321), myService.measure(new MyBean(321, "321")));
            assertEquals(Integer.valueOf(0), myService.measure(new MyBean(777, "777")));
        }
    
        @Test
        public void testThatPasses(){
    
            doReturn(123).when(myService).measure(argThat((mb) -> Objects.equals(mb.id, 123) && Objects.equals(mb.name, "123")));
            doReturn(321).when(myService).measure(argThat((mb) -> Objects.equals(mb.id, 321) && Objects.equals(mb.name, "321")));
    
            assertEquals(Integer.valueOf(123), myService.measure(new MyBean(123, "123")));
            assertEquals(Integer.valueOf(321), myService.measure(new MyBean(321, "321")));
            assertEquals(Integer.valueOf(0), myService.measure(new MyBean(777, "777")));
        }
    

    空指针是一个遗憾,在需要的地方使用可选的。好吧,我在可能的地方这样做。然而,我找不到任何理由说明mockito在第二次调用
    时抛出和NPE。这太糟糕了……更新:第二次调用
    Mockito的斗争。当
    是一个知识时,mocked方法可以在一个单元测试中被多次调用。我还没有挖掘出源代码来真正理解为什么,我也没有在中找到很多,但基本上与。Szczepan(Mockito的作者)也没有提供任何见解。简而言之,将第二个
    when
    更改为
    doReturn(3).when(iface).callMe(…)你会很好的。请注意,除非您实际使用第二个stubing,否则您的测试将通过,但您将在测试时得到一个
    不必要的stubbingException
    end@Morfic这肯定是一个答案…空指针是一个耻辱,在需要的地方使用可选的。好吧,我在可能的地方这样做。然而,我找不到任何理由说明mockito在第二次调用
    时抛出和NPE。这太糟糕了……更新:第二次调用
    Mockito的斗争。当
    是一个知识时,mocked方法可以在一个单元测试中被多次调用。我还没有挖掘出源代码来真正理解为什么,我也没有在中找到很多,但基本上与。Szczepan(Mockito的作者)也没有提供任何见解。简而言之,将第二个
    when
    更改为
    doReturn(3).when(iface).callMe(…)你会很好的。请注意,除非您实际使用第二个stubb
    
    class MyBean{
        public Integer id;
        public String name;
    
        public MyBean(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
    
    interface MyService{
        Integer measure(MyBean myBean);
    }
    
        @Test
        public void testThatFailsWithNPE(){
    
            when(myService.measure(argThat((mb) -> Objects.equals(mb.id, 123) && Objects.equals(mb.name, "123")))).thenReturn(123);
            when(myService.measure(argThat((mb) -> Objects.equals(mb.id, 321) && Objects.equals(mb.name, "321")))).thenReturn(321);
    
            assertEquals(Integer.valueOf(123), myService.measure(new MyBean(123, "123")));
            assertEquals(Integer.valueOf(321), myService.measure(new MyBean(321, "321")));
            assertEquals(Integer.valueOf(0), myService.measure(new MyBean(777, "777")));
        }
    
        @Test
        public void testThatPasses(){
    
            doReturn(123).when(myService).measure(argThat((mb) -> Objects.equals(mb.id, 123) && Objects.equals(mb.name, "123")));
            doReturn(321).when(myService).measure(argThat((mb) -> Objects.equals(mb.id, 321) && Objects.equals(mb.name, "321")));
    
            assertEquals(Integer.valueOf(123), myService.measure(new MyBean(123, "123")));
            assertEquals(Integer.valueOf(321), myService.measure(new MyBean(321, "321")));
            assertEquals(Integer.valueOf(0), myService.measure(new MyBean(777, "777")));
        }