java - One assert statement tests several conditions -


i found many information bad practices use several asserts in 1 method (one test condition). it's clear. each method should test 1 , 1 condition. should 1 assert in cases.

but can't find information best practices situation, if one assert tests several conditions.

example:

@test public void findallperformers_returnsperformersinalphabeticalorder() {     list<string> performers = reportrepository.findallperformers();     assertthat("should return performers in alphabetical order",             performers, contains("bart simpson", "homer simpson",                     "ned flanders", "xena warrior princess")); } 

first, had have 2 more test methods (conditions): findallperformers_returnsalluniqueperformers , findallperformers_returnseachperformeronlyonce. realized first method tests 2 conditions too!

so, should do?

  1. leave methods is.
  2. remove 2 other test methods , rename findallperformers_returnsperformersinalphabeticalorder method long name 3 conditions included (because tests 3 conditions!)
  3. just remove 2 other test methods.
  4. leave methods invoke findallperformers_returnsperformersinalphabeticalorder method 2 other methods.
  5. copy-paste findallperformers_returnsperformersinalphabeticalorder method contents 2 other methods.
  6. anything else...

if test fails person looking @ failed test see findallperformers_returnsperformersinalphabeticalorder(). looking @ alone appears method under test returned list wasn't in alphabetical order, not list didn't contain 'bart simpson'.

avoid options 4 , 5, don't solve , increase duplication/maintenance without added value (since tests fail reasons not mentioned in test name). instead try find way test want assert.