Weird problem using JUnit in multi-thread environment

前端 未结 5 1040
耶瑟儿~
耶瑟儿~ 2020-12-16 05:03

I meet a weired problem when using JUnit in multi-thread environment. The following code should fail, but it actually pass in eclipse.

public class ExampleT         


        
5条回答
  •  再見小時候
    2020-12-16 05:32

    JUnit cannot see the exceptions that occur in threads other than the thread in which the tests are running. In the first case, through an exception occurs by calling fail, it occurs in a separate thread run by the executor. Hence it is not visible to JUnit and the test passes.

    In the second case, the same exception happens in the separate thread run by the executor but the exception is effectively "reported back" to the test thread when you call future.get. This is because future.get throws an ExecutionException if the computation of the future failed due to any exception. JUnit is able to see this exception and hence the test fails.

提交回复
热议问题