How to capture a list of specific type with mockito

后端 未结 8 1586
天命终不由人
天命终不由人 2020-12-12 10:05

Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn\'t work:

ArgumentCaptor> argumen         


        
相关标签:
8条回答
  • 2020-12-12 11:06

    If you're not afraid of old java-style (non type safe generic) semantics, this also works and is simple'ish:

    ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class);
    verify(subject.method(argument.capture()); // run your code
    List<SomeType> list = argument.getValue(); // first captured List, etc.
    
    0 讨论(0)
  • 2020-12-12 11:11

    Based on @tenshi's and @pkalinow's comments (also kudos to @rogerdpack), the following is a simple solution for creating a list argument captor that also disables the "uses unchecked or unsafe operations" warning:

    @SuppressWarnings("unchecked")
    final ArgumentCaptor<List<SomeType>> someTypeListArgumentCaptor =
        ArgumentCaptor.forClass(List.class);
    

    Full example here and corresponding passing CI build and test run here.

    Our team has been using this for some time in our unit tests and this looks like the most straightforward solution for us.

    0 讨论(0)
提交回复
热议问题