Read how to customize code templates for JUnit 4 test, setup and teardown methods in IntelliJ.
Wednesday, July 24, 2013
Monday, July 15, 2013
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
Sunday, July 14, 2013
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).
I have been working with unit testing for several years and I always had problems in finding the best names for my tests. I have been trying different conventions and I ended up with the practice of naming tests based on the behavior they test.