2016-12-13 1 views
0

スレッドからスローされたRunTimeExceptionがSystem.errに追加されているかどうかテストしようとしています。 しかし、たとえ1分間待った後、それが後をSystem.err に見つからない場合は、誰かが、テストで可能間違いを指摘してくださいすることができ、コード片をRunTimeExceptionスレッドからSystemErrRuleにログインしていません

public class TestTest { 
    @Rule public SystemErrRule errRule = new SystemErrRule().enableLog(); 

    @org.junit.Test 
    public void testLogging(){ 
    ExecutorRunner.submitTask(new Runnable() { 
     @Override public void run() { 
     throw new RuntimeException("exception"); 
     } 
    }); 

Awaitility.await().atMost(60l, TimeUnit.SECONDS).until(new Callable<Boolean>() { 
    @Override public Boolean call() throws Exception { 
    return errRule.getLog().contains("exception"); 
    } 
}); 

System.out.println("TestTest.testLogging :: " + "errLog: "+errRule.getLog()); 
assertTrue(errRule.getLog().contains("exception")); 


} 

    @After 
    public void checkException(){ 
    System.out.println("TestTest.checkException :: " + "checkin log"); 
    } 

} 

class ExecutorRunner{ 
    static final ExecutorService execService = Executors 
    .newCachedThreadPool(new ThreadFactory() { 
     AtomicInteger threadNum = new AtomicInteger(); 

     public Thread newThread(final Runnable r) { 
     Thread result = new Thread(r, "Function Execution Thread-" 
      + threadNum.incrementAndGet()); 
     result.setDaemon(true); 
     return result; 
     } 
    }); 

    static void submitTask(Runnable task) { 
    execService.submit(task); 
    } 
} 

のですか?

答えて

0

あなたもSystemErrRuleがSystem.errに書き込みをインターセプトので、あなたはこのようにこれを行う必要があります

e.printStackTrace(); 

System.err.print("exception"); 

を使用することができます

ExecutorRunner.submitTask(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       throw new RuntimeException("exception"); 
      } catch (RuntimeException e) { 
       System.err.print("exception"); 
      } 
     } 
    }); 

にごRunnableを実装を更新。 printStackTrace()はあなたのためにそれを行うことができますか、System.errを使用して手動で行うことができます

関連する問題