Creating Deep Stubs With Mockito to Chain Method Stubbing

Yet another blog post about a Mockito feature that we should rarely use: Mockito deep stubs. With this article, we’ll explore how deep stubbing can reduce the boilerplate stub setup for our tests when chaining fluent APIs of a mocked class. In general, “Every time a mock returns a mock, a fairy dies” should be

Read More

Mock Java Constructors With Mockito | Configuration and Examples

Starting with Mockito version 3.5.0, we can now mock Java constructors with Mockito. This allows us to return a mock from every object construction for testing purposes. Similar to mocking static method calls with Mockito, we can define the scope of when to return a mock from a Java constructor for a particular Java class.

Read More

@Mock vs. @MockBean When Testing Spring Boot Applications

When you start testing your Spring Boot application sooner or later you’ll stumble over @Mock and @MockBean. Both annotations create mock objects but with a slightly different purpose. This might be confusing in the beginning. With this blog post, I’ll resolve this confusion and explain the difference between @Mock and @MockBean when it comes to

Read More

Mocking Static Methods with Mockito (Java & Kotlin)

Want to start a religious discussion? Start asking your teammates whether or not you should mock static method calls. It’s debatable if this is an absolute no-go or sometimes a last resort. I agree that you should rethink your implementation if you find yourself googling: Mocking static calls Java. On the other side, there are

Read More

Guide to Testing With the Spring Boot Starter Test

With Spring Boot, you only need one dependency to have a solid testing infrastructure: Spring Boot Starter Test. Using this starter, you don’t need to upgrade testing libraries and keep them compatible manually. You’ll get an opinionated set of libraries and can start writing tests without further setup effort. This guide gives you a first

Read More