Wednesday, July 24, 2013

Monday, July 15, 2013

8 ways of handling exceptions in JUnit. Which one to choose?

In JUnit there are many ways of handling exceptions in your test code:
  • try-catch idiom
  • With JUnit rule
  • With @Test annotation
  • With catch-exception library
  • With custom annotation
  • With Lambda expression (as of Java 1.8)
  • With AssertJ 3.0.0 for Java 8
  • (NEW!) With JUnit 5 built-in assertThrows
Which one should we use and when?

Sunday, July 14, 2013

Test code readability improved: JUnit with Mockito and FEST Fluent Assertions

To improve the readability of my unit tests I use assertThat with Hamcrest matchers. This is a good way to improve readability of your test code, especially when I statically import members of like org.junit.Assert.assertThat and org.hamcrest.Matchers. But when I add Mockito matchers on top of it, I experienced the problem of static import conflicts that ended up with "not nice" code (according to my definition).