1

I would like to write a test in which I pass in as an argument a list of values, and I assert that 4 different methods are called using each value. For example

@ParameterizedTest
@SomeKindOfSource
public void runTest(List<Integer> ints){
  for (int i : ints){
    assertAll(
      () -> verify(myMock, times(1)).method1(eq(i)),
      () -> verify(myMock, times(1)).method2(eq(i)),
      () -> verify(myMock, times(1)).method3(eq(i)),
      () -> verify(myMock, times(1)).method4(eq(i))
    );
  }
}

This asserts that, for each element, all 4 methods are called once. However, if the first element fails, the test short-circuits and the remainder are not called.

How can I pass a single list of executables into the assertAll method, such that the test verifies all for assertions for all 4 values before failing if there are any failures?

3
  • 1
    The whole idea of JUnit is that a test method either passes or fails, and that the failure shows you only the first failed assertion. If you need to see separate results for each assertion, then you should really set them up as four separate test methods. Commented Oct 14, 2024 at 21:21
  • What result you want to achieve? Fail only of all verify fails? Commented Oct 14, 2024 at 21:22
  • This question is similar to: JUnit5 assertAll. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 14, 2024 at 21:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.