junit - Failing a unit test if an exception is thrown in another thread -


currently, whenever need fail test in response exception thrown in thread, write this:

package com.example;  import java.util.arraylist; import java.util.list; import org.testng.annotations.test;  import static java.util.arrays.aslist; import static java.util.collections.synchronizedlist; import static org.testng.assert.fail;  public final class t {   @test   public void testfailurefromlambda() throws throwable {     final list<throwable> errors = synchronizedlist(new arraylist<>());      aslist("0", "1", "2").parallelstream().foreach(s -> {       try {         /*          * actual code under test here.          */         throw new exception("error " + s);       } catch (final throwable t) {         errors.add(t);       }     });      if (!errors.isempty()) {       errors.foreach(throwable::printstacktrace);        final throwable firsterror = errors.iterator().next();       fail(firsterror.getmessage(), firsterror);     }   } } 

a synchronized list may replaced atomicreference<throwable>, in general code remains pretty same.

is there standard (and less verbose) way of doing same using of test frameworks available in java (testng, junit, hamcrest, assertj, etc.)?

by default testng fails test method when exception thrown it. believe same thing happens junit well, wherein marks test errored, if throws unexpected exception.

if dealing streams, need wrap within runtimeexception variant, java doesn't complain. testng automatically fail test.

here's sample :

@test public void testfailurefromlambdarefactored() {     aslist("0", "1", "2").parallelstream().foreach(s -> {         try {         /*         * actual code under test here.         */             if (s.equals("2")) {                 throw new exception("error " + s);             }         } catch (final throwable t) {             throw new runtimeexception(t);         }     }); } 

this scenarios involve lambdas , streams. in general if know exception happens in new thread spun off @test method, need use executorservice.

here's sample :

@test public void testfailureinanotherthread() throws interruptedexception, executionexception {     list<string> list = aslist("0", "1", "2");     executorservice service = executors.newfixedthreadpool(2);     list<future<void>> futures = service.invokeall(arrays.aslist(new worker(list)));     (future future : futures) {         future.get();     }  }  public static class worker implements callable<void> {     private list<string> list;      public worker(list<string> list) {         this.list = list;     }      @override     public void call() throws exception {         (string s : list) {             if (s.equals("2")) {                 throw new exception("error " + s);             }         }         return null;     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -