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.


Popular posts from this blog

Tuning ext4 for performance with emphasis on SSD usage

Rust’s impact on the industry

Brandan Eich on WebAssembly, Web Workers, and Decorators