Search code examples
javaassert

One assert with multiple condition vs multiple assert


In java, If I need to ensure multiple conditions, I can write in two ways as using single assert with all condition vs using separate assert for each condition. Which one is better approach?

Example: Suppose we have three condition need to be ensured as cond1, cond2, cond3.

Using single assert

assert cond1 && cond2 && cond3;

Using multiple assert

assert cond1; assert cond2; assert cond3;


Solution

  • Multiple assert statements. One for each condition will help you easily determine which condition failed, in case there's a problem.

    With a single assertion statement for all conditions, you will have to as a next step identify which condition failed. As test will just say that assertion failed but what exactly failed still needs to be determined.