2016-12-13 2 views
0

Я пытаюсь проверить, добавлено ли RunTimeException из потока добавлено в System.err. Но даже после ожидания в течение 1 мин не обнаруживается в System.err Ниже кусок кодаRunTimeException from Thread не вошел в систему 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

Обновите Runnable реализация в

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

вы также можете использовать

System.err.print("exception"); 

в

e.printStackTrace(); 

Вы должны сделать это так, потому что SystemErrRule перехватывает запись на System.err , printStackTrace() может сделать это за вас, или вы можете сделать это вручную, используя System.err

Смежные вопросы