Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 2
    And that's a good thing, you shouldn't have any conditional logic in your tests. It makes test more complex and less readable. And I guess Roy outlined it right in his blog post that multiple asserts on one object is okay most of the time. So those that you have are just guard asserts and it's okay to have them. Commented Oct 30, 2012 at 20:44
  • 2
    Your Assert.notNulls are redundant, your test will fail with an NPE if they're null. Commented Jun 2, 2016 at 10:01
  • 1
    Also, your first example (with the if) will pass if res is null Commented Jun 2, 2016 at 10:02
  • 3
    @kai notNull's are redundant, I agree, but I feel that it is cleaner to have the assert (and if I'm not lazy also with proper message) instead of exception... Commented Jun 2, 2016 at 13:50
  • 1
    Well a failed assertion also throws an exception, and in both cases you get a direct link to the exact row that threw it with an accompanying stack trace so personally I'd rather not clutter the test with preconditions that will get checked anyway. I'd prefer a oneliner à la Assert.assertEquals(..., service.process().getInner());, possible with extracted variables if the line gets "too long" Commented Jun 2, 2016 at 13:53