简体   繁体   中英

How can I stub a method that takes a Generic class as a param?

I have a class under test that needs to stub out a call on a dependent class. The code that needs to be stubbed looks like this:

public class A {
 ...
 public void methodThatNeedsToBeStubbed(GenericClass genericClass){
    ...
 }
}

This doesn't compile:

when(mockA.methodThatNeedsToBeStubbed(any(GenericClass<SomeDifferentClass>))

I'm not sure how to get this to work with a generic class as a param?

Try using an ArgumentCaptor with @Captor .

 @Captor
 ArgumentCaptor<GenericClass<SomeDifferentClass>> captor;

 @Before
 public void setup(){ MockitoAnnotations.initMocks(this));}

 @Test
 public void test(){
   when(mockA.methodThatNeedsToBeStubbed(captor.capture()))...
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM