Entries

Different ways of validating @RequestBody in Spring MVC with @Valid annotation

In Spring MVC the @RequestBody annotation indicates a method parameter should be bound to a body of the request. @RequestBody parameter can treated as any other parameter in a @RequestMapping method and therefore it can also be validated by a standard validation mechanism. In this post I will show 3 ways of validating the @RequestBody parameter in your Spring MVC application.

HOW-TO: Customize code templates for JUnit 4 test, setup and teardown methods quickly in IntelliJ

Read how to customize code templates for JUnit 4 test, setup and teardown methods in IntelliJ.

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?

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).

Unit test names describe features

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.