2017-02-06 30 views
0

我正在使用PowerMock,我看到使用@InjectMock我可以在我的測試中獲得注入類。阿卡演員模擬注入類

但我需要的是,使用已注入類的Akka actor,對該actor執行測試並在其中注入模擬類。

class A extends Actor{ 

    @Inject private B b;//How can I mock this class? 

} 

@Test 
public test(){ 
      final Props props = Props.create(A.class, new A()); 
     testActorRef = TestActorRef.create(actorSystem, props); 
      Future<Object> ask = Patterns.ask(testActorRef); 


} 

只是爲了澄清源代碼無法修改。

回答

0

最簡單的方法是改變構造器注入:

class A extends Actor{ 
    private final B b;//How can I mock this class? 
    A(@Inject B b){ 
    this.b=b; 
    } 
} 

導致這個測試:

@Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); 

@Mock private B mockedB; 

@Test 
public test(){ 
      // configure mockedB here 
      A a = new A(mockedB); 

      // do something with a 
      // verify method invocations on b 
} 
+0

我改變的問題,只怕源代碼不能被修改 – paul