-
Bug
-
Resolution: Done
-
Minor
-
18.0.0.Beta2
-
None
-
None
Only one method invocation is expected when testing exceptions. Refers to [RSPEC-5783|https://rules.sonarsource.com/java/RSPEC-5783, RSPEC-5778
Sample:
public void testBad() throws Throwable { try { // Test preparation - no Exception expected // Test run - Exception expected fail("Should not have been rechead"); } catch (AssertionError ae) { throw ae; // In worst case, this is missing - Test never fails. } catch (Throwable t) { // Expected, test success. Broken test preparation is ignored, too. } finally { // Clean up here } }
public void testRefactored() throws Throwable {
Throwable expected = null;
try {
// Test preparation - no Exception expected
try {
// Test run - Exception expected
} catch (Throwable ex) {
expected = ex;
}
} catch (Throwable t) {
// optional cleanup
throw t; // Test preparation failed
} finally {
// Clean up here
}
assertNotNull("Exception was expected, not succes", expected);
}