junit assert in thread throws exception

后端 未结 5 1394
再見小時候
再見小時候 2020-12-28 15:10

What am I doing wrong that an exception is thrown instead of showing a failure, or should I not have assertions inside threads?

 @Test
 public void testCompl         


        
5条回答
  •  我在风中等你
    2020-12-28 15:48

    I ended up using this pattern it work with both Runnables and Threads. It is largely inspired from the answer of @Eyal Schneider:

    private final class ThreadUnderTestWrapper extends ThreadUnderTest {
        private Exception ex;
    
        @Override
        public void run() {
            try {
                super.run();
            } catch ( Exception ex ) {
                this.ex = ex;
            }
        }
    
        public Exception getException() throws InterruptedException {
            super.join(); // use runner.join here if you use a runnable. 
            return ex;
        }
    }
    

提交回复
热议问题