How to test Akka Actor functionality by mocking one or more methods in it

后端 未结 4 1649
旧巷少年郎
旧巷少年郎 2021-02-04 10:34

I\'m interested to know about how to test Akka Actor functionality, by mocking some methods (substitute real object\'s/actor\'s method implementation by moc

4条回答
  •  后悔当初
    2021-02-04 11:23

    To mock an actor is easier through the TestActorRef. You can use this code :

    static ActorSystem system = ActorSystem.create();
    static Props propsSome = Props.create(MockedResultActor.class);
    
    TestActorRef refMockedResultActor= TestActorRef.create(
                    system, propsSome, "testA");
    
    // Mocking an actor class and returning our reference actor
    PowerMockito.mockStatic(ClassToBeMocked.class);
    Mockito.when(ClassToBeMocked.getToBeMockedMethod())
                    .thenReturn(refMockedResultActor);
    

    Note: ClassToBeMocked--Its a class you want to mock. MockedResultActor -- Its a class you want to return after mocking. This can be run using JunitTest after implementing basic configuration of mocking in your class. Code given here is specific to akka actor in java only.

提交回复
热议问题