junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.53k stars 3.28k forks source link

Removing through iterator not supported by CopyOnWriteArrayList #209

Closed krosenvold closed 12 years ago

krosenvold commented 13 years ago

Run-listener throwing exception causes this, and it can be seen that removing an element by its iterator is not suppored on copyonwritearraylist.

Caused by: java.lang.IllegalStateException at java.util.AbstractList$Itr.remove(AbstractList.java:356) at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:43) at org.junit.runner.notification.RunNotifier.fireTestRunFinished(RunNotifier.java:68) at org.junit.runner.JUnitCore.run(JUnitCore.java:158) at org.junit.runner.JUnitCore.run(JUnitCore.java:136) at org.junit.runner.JUnitCore.run(JUnitCore.java:127) at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:51) at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:110)

Fix with the following change or similar: for (Iterator all = fListeners.iterator(); all.hasNext();){ RunListener next = all.next(); try { notifyListener(next); } catch (Exception e) { fListeners.remove( next); // Remove the offending listener first to avoid an infinite loop fireTestFailure(new Failure(Description.TEST_MECHANISM, e)); } }

dsaff commented 13 years ago

Nice bug catch. Thanks!

Stephan202 commented 12 years ago

For reference: there are other issues with this code, please see #395.