How to use JUnit to test asynchronous processes

前端 未结 18 1651
小蘑菇
小蘑菇 2020-11-29 15:06

How do you test methods that fire asynchronous processes with JUnit?

I don\'t know how to make my test wait for the process to end (it is not exactly a unit test, it

18条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 15:49

    An alternative is to use the CountDownLatch class.

    public class DatabaseTest {
    
        /**
         * Data limit
         */
        private static final int DATA_LIMIT = 5;
    
        /**
         * Countdown latch
         */
        private CountDownLatch lock = new CountDownLatch(1);
    
        /**
         * Received data
         */
        private List receiveddata;
    
        @Test
        public void testDataRetrieval() throws Exception {
            Database db = new MockDatabaseImpl();
            db.getData(DATA_LIMIT, new DataCallback() {
                @Override
                public void onSuccess(List data) {
                    receiveddata = data;
                    lock.countDown();
                }
            });
    
            lock.await(2000, TimeUnit.MILLISECONDS);
    
            assertNotNull(receiveddata);
            assertEquals(DATA_LIMIT, receiveddata.size());
        }
    }
    

    NOTE you can't just used syncronized with a regular object as a lock, as fast callbacks can release the lock before the lock's wait method is called. See this blog post by Joe Walnes.

    EDIT Removed syncronized blocks around CountDownLatch thanks to comments from @jtahlborn and @Ring

提交回复
热议问题