2017-11-30 1 views
0

를 사용하는 함수입니다 인수 내가는 ArgumentCaptor의 mockito

Somefun(){ 

    mockedService.execute(()->{ 
     //function body 
    }) 

} 

을 캡처하는 방법 그래서이 모의 내부의 방법을 실행 실행하려는. 그렇게하려면 어떻게해야합니까? 나는 어떻게 든이 모의 (함수 임)의 주장을 포착하고 그것을 실행하면 내 작업이 완료 될 것이라고 생각했다. 이것을 달성 할 수있는 방법이 있습니까? 또는 다른 방법. 감사합니다.

답변

2

그것은 다음과 같이 보일 것입니다 :

@RunWith(MockitoRunner.class) 
public class SomeFunTest { 
    @Mock Service mockedService; 
    @Captor ArgumentCaptor<Runnable /* or whatever type of function you expect there*/> functionCaptor; 

    @Test 
    public void serviceShouldInvokeFunction() { 
     // given 
     ObjectUnderTest out = new ObjectUnderTest(mockedService); 

     // when 
     out.SomeFun(); 

     // then 
     verify(mockedService).execute(captor.capture()); 

     /* Do your assertions after you captured the value */ 
     assertThat(captor.getValue()).isInstanceOf(Runnable.class); 
    } 
} 
+0

쿨, 어떻게 든 난 완전히 지금까지 @Captor를 놓쳤다. 좋은 힌트, 고마워. –