You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geode.apache.org by Kirk Lund <kl...@apache.org> on 2016/09/27 18:54:17 UTC

JUnit best practices #1

Some of the very basic best practices for JUnit:

1) never catch an unexpected exception

Yes:
public void myTest() throws Exception {
  doSomething();
  validation();
}

No:
public void myTest() {
  try {
    doSomething();
    validation();
  } catch (Exception e) {
    fail(e.getMessage());
  }
}

Why: because the 2nd example makes the test harder to read and it also
disguises the stack trace if it fails. JUnit framework handles this 100%
better than "fail(e.getMessage())".

Please don't use this testing antipattern.

[1] http://www.exubero.com/junit/antipatterns.html
[2] http://junit.org/junit4/faq.html#atests_8

-Kirk