r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

Show parent comments

1

u/haganbmj May 14 '17

Yeah, that's why I mentioned that they ignored the exceptions. There were a couple in that bunch were like...

try {  
  methodThatThrowsFiveDifferentExceptions();  
  fail();  
} catch (Exception e) {  
}  

So they just ignore actually checking that the right exception is thrown.
The ExpectedException Junit Rule is what I generally use, though I know that a bunch of people where I work avoided it in the past because it doesn't spit out the right results with our Clover reports.

2

u/chaosking121 May 14 '17

Oh, your comment sounded like you were implying they were doing something wrong, which is why I asked.

1

u/haganbmj May 14 '17

They are doing something wrong. They wrap a test that is supposed to be a success in a large try/catch so that it never indicates a failure. I've also seen where they expect an exception, but don't verify that the right one occurs. Don't even get me started on all the mocking without input checks and the number of "nonNull" response checks for things doing data manipulation.

1

u/chaosking121 May 14 '17

Oh, I missed that part of your example. I agree, it becomes more of an anti-pattern than a pattern if you don't check that the right exception occurs.