代码之家  ›  专栏  ›  技术社区  ›  user2572526

PowerMockito如何捕获传递给模拟对象的参数?

  •  3
  • user2572526  · 技术社区  · 7 年前

    //I create a mock object
    ClassMocked mock = PowerMockito.mock(ClassMocked.class);
    
    //Create the captor that will capture the String passed in input to the mock object
    ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
    
    //When the method put is executed on my mock I want the second parameter (that is a String) to be captured
    Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());
    
    //Creates the instance of the class that I want to test passing the mock as parameter
    ClassToTest instance = new ClassToTest(mock);
    
    //Executes the method that I want to test
    instance.methodToTest();
    
    /* I know that during the execution of "methodToTest()" mock.put(String,String) will be executed and I want to get the second string parameter. */
    

    需要但未调用mock.put(任意,捕获参数)

    怎么了?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Alain-Michel Chomnoue N    7 年前

    你应该打电话 instance.methodToTest(); Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());

        2
  •  1
  •   GhostCat    7 年前

    verify() 验证 指定的方法调用 发生

    因此: 需要发生 ,而不是之前!