2012-03-16

How to unit test async code with callbacks

@Testpublic void deleteEmployee() throws Exception {    EmployeeReqCtx reqCtx = rf.employeeReqCtx();    final boolean[] onSuccess = {false};    reqCtx.delete(10L).fire(new Receiver<Void>() {        @Override        public void onSuccess(Void response) {            onSuccess[0] = true;        }
        @Override        public void onFailure(ServerFailure failure) {            System.err.println("failure.getMessage() = " + failure.getMessage());        }    });    assertTrue(onSuccess[0]);}
The trick is to use a final boolean array because you need to use values (finals) within callbacks (inner classes) and finals are immutable so you could not simply change a final onSuccess value to true.