Now, when executing any unit test, Junit displays only the first error that occurred (if assert was detected). How to set up aggregation of errors so that they accumulate at runtime, and then at the very end they are thrown away at once?

  • 2
    If you mean assertion aggregation in one test , then it is physically and morally impossible. Physically - because an exception is thrown there, morally - because the code after the first broken statement is probably wrong, because operates with unreliable data. - etki
  • @Etki This is necessary to understand the overall picture: a test that has completely fallen out, and a test with a small blot - give the same information to the developer, which can be very confusing. - TheSN
  • There is no “small blot” in the tests, there is only a Boolean logic “it works according to the specification or not”. If you have too much test, then this is a separate question. - etki
  • @Etki, yeah, integration tests for Junit work for us and run every problem 20 times, receiving batch errors - the option is bad. - TheSN
  • If you answer shortly, then you a) need Allure ( steps ) and b) forbid colleagues to write tests for more than one feature - etki

1 answer 1

public class AssertsAccumulator { private boolean assertsPassed; private StringBuilder errors; /** * Initializes a new instance of the class. * <pre> * {@code AssertsAccumulator assertsAccumulator = new AssertsAccumulator(); * assertsAccumulator.accumulate(() -> assertNotNull(null)); * assertsAccumulator.release(); * } </pre> * @see AssertsAccumulator */ public AssertsAccumulator() { this.errors = new StringBuilder(); this.assertsPassed = true; } /** * Gets Regularly updates progress on a given task. * Should be invoked every time task.ProgressValue or task.MaxProgressValue changes */ public String getAccumulatedErrorMessage() { return this.errors.toString(); } /** * value indicating whether assert passed or failed. * @return <c>true</c> if assert passed, <c>false</c> otherwise. */ public boolean isAssertsPassed() { return assertsPassed; } /** * Regularly updates progress on a given task. * Should be invoked every time task.ProgressValue or task.MaxProgressValue changes */ public void release() { if (!this.assertsPassed) { throw new AssertionError(this.getAccumulatedErrorMessage()); } } /** * updates progress on a given task. * be invoked every time task.ProgressValue or task.MaxProgressValue changes * * @param action Action for run */ public void accumulate(Runnable action) { try { action.run(); } catch (AssertionError e) { this.registerError(e); } catch (Exception exception) { this.registerError(exception); } } public void accumulate(Runnable action, String key) { try { action.run(); } catch (AssertionError e) { this.registerError( } catch (Exception exception) { this.registerError(exception); } } /** * Regularly updates progress on a given task. * Should be invoked every time task.ProgressValue or task.MaxProgressValue changes */ private void registerError(Throwable e) { this.assertsPassed = false; this.errors.append(e.getMessage() + "\n" + stackTraceToString(e)); this.errors.append(Messages.getLineSeperator("~", 40)); this.errors.append(System.lineSeparator()); } private String stackTraceToString(Throwable e) { StringBuilder sb = new StringBuilder(); int k = 0; for (StackTraceElement element : e.getStackTrace()) { sb.append(element.toString()); sb.append("\n"); if (k > 5) break; k++; } return sb.toString(); } 

use so

 AssertsAccumulator accumulator = new AssertsAccumulator(); accumulator.accumulate(() -> Assert.fail())); accumulator.accumulate(() -> Assert.fail("Message"))); accumulator.release(); 
  • In Intellij Idea, the informative output comparison result will work for every error, do you happen to know? - TheSN
  • did not understand the question a little. Could you clarify? - Senior Pomidor
  • org.junit.ComparisonFailure: Files do not match: C: \ ... \ actual_file C: \ ... \ expected_file <Click to see difference> when clicked on Click to see difference IDEA shows a side-by-side window, where you can see the specific location of the discrepancy. - TheSN
  • No, since ComparisonFailure inherited from AssertionError , which is caught in catch . so it will be caught in the AssertsAccumulator - Senior Pomidor
  • Thank you for help! - TheSN