2.10 在verification中捕获调用参数

作者: 孙兴斌 | 来源:发表于2016-12-27 17:37 被阅读32次

    单次调用捕获

    使用withCapture()捕获最后一次调用的参数。

    @Test
    public void capturingArgumentsFromSingleInvocation(@Mocked final Collaborator mock)
    {
       new Collaborator().doSomething(0.5, new int[2], "test");
    
       new Verifications() {{
          double d;
          String s;
          mock.doSomething(d = withCapture(), null, s = withCapture());
    
          assertTrue(d > 0.0);
          assertTrue(s.length() > 1);
       }};
    }
    

    多次调用捕获

    使用withCapture(List)捕获所有参数。

    @Test
    public void capturingArgumentsFromMultipleInvocations(@Mocked final Collaborator mock)
    {
       mock.doSomething(dataObject1);
       mock.doSomething(dataObject2);
    
       new Verifications() {{
          List<DataObject> dataObjects = new ArrayList<>();
          mock.doSomething(withCapture(dataObjects));
    
          assertEquals(2, dataObjects.size());
          DataObject data1 = dataObjects.get(0);
          DataObject data2 = dataObjects.get(1);
          // Perform arbitrary assertions on data1 and data2.
       }};
    }
    

    捕获新实例

    使用withCapture(new XX())

    @Test
    public void capturingNewInstances(@Mocked Person mockedPerson) {
       new Person("Paul", 10);
       new Person("Mary", 15);
       new Person("Joe", 20);
    
       new Verifications() {{
          List<Person> personsInstantiated = withCapture(new Person(anyString, anyInt));
       }};
    }
    

    相关文章

      网友评论

        本文标题:2.10 在verification中捕获调用参数

        本文链接:https://www.haomeiwen.com/subject/nddevttx.html