非同期メソッドを JUnit でテストする方法

力技

@Test
public void testSend() throws Exception {
    final BlockingQueue<Object> result = new SynchronousQueue<Object>();
    
    new ActorObject(driver) {
        @Override
        protected void exec(String func, Object... args) {
            try {
                result.put(func);
                result.put(args);
            } catch(InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }
    }.send("sample", 1, 'a', "abc");
    
    String func = (String) result.take();
    Object[] args = (Object[]) result.take();
    
    assertTrue(func.equals("sample"));
    assertTrue(args.length == 3);
    assertTrue(args[0].equals(1));
    assertTrue(args[1].equals('a'));
    assertTrue(args[2].equals("abc"));
}