LooperActorThread implements ActorThread { @NonNull @Override public <T> ActorRef<T> bind(@NonNull Class<T> cls, @NonNull T impl) { MessageSender postman = new LooperMessageSender<>(mLooper, mFailureHandler, impl); T instance = mProxyFactory.newProxy(cls, postman); return new ActorRefImpl<>(instance); } LooperActorThread(@NonNull ProxyFactory proxyFactory, @NonNull FailureHandler failureHandler,
                                    @NonNull Looper looper); @NonNull @Override ActorRef<T> bind(@NonNull Class<T> cls, @NonNull T impl);  }
@SuppressWarnings("unchecked") @Test public void messageDeliverTest() throws Exception { final Message message = Mockito.mock(Message.class); final LooperActorThread actorThread = new LooperActorThread(mProxyFactory, mFailureHandler, mLooper); ActorRef<Runnable> runnableRef = actorThread.bind(Runnable.class, mActorImpl); Assert.assertSame(mProxy, runnableRef.tell()); ArgumentCaptor<MessageSender> postmanCaptor = ArgumentCaptor.forClass(MessageSender.class); Mockito.verify(mProxyFactory).newProxy(Mockito.<Class>any(), postmanCaptor.capture()); MessageSender postman = postmanCaptor.getValue(); postman.deliver(message); Mockito.verify(message, Mockito.never()).invoke(Mockito.any()); mShadowLooper.runToEndOfTasks(); Mockito.verify(message).invoke(mActorImpl); }
@SuppressWarnings("unchecked") @Test public void messageExceptionTest() throws Exception { final Throwable occurredException = new RuntimeException(); final ActorMethodInvokeException baseCallException = Mockito.mock(ActorMethodInvokeException.class); final Message message = Mockito.mock(Message.class); Mockito.doThrow(occurredException).when(message).invoke(mActorImpl); Mockito.doReturn(baseCallException).when(message).exception(); final LooperActorThread actorThread = new LooperActorThread(mProxyFactory, mFailureHandler, mLooper); ActorRef<Runnable> runnableRef = actorThread.bind(Runnable.class, mActorImpl); Assert.assertSame(mProxy, runnableRef.tell()); ArgumentCaptor<MessageSender> postmanCaptor = ArgumentCaptor.forClass(MessageSender.class); Mockito.verify(mProxyFactory).newProxy(Mockito.<Class>any(), postmanCaptor.capture()); MessageSender postman = postmanCaptor.getValue(); postman.deliver(message); Mockito.verify(message, Mockito.never()).invoke(Mockito.any()); mShadowLooper.runToEndOfTasks(); Mockito.verify(mFailureHandler).onUncaughtException(mActorImpl, message, baseCallException); Mockito.verify(baseCallException).initCause(occurredException); }