Posts

Showing posts from February, 2023

Mocking with Spring

Hi folks, Been diving deep into Spring Testing lately, and today I'm sharing my learnings about mocking in Spring. I've noticed many developers struggle with this, so let's break it down in a simple way. What is Mocking? Mocking is our way of creating objects that simulate the behaviour of real objects during testing. Rather brilliant, eh? When testing complex systems, we don't want to call actual databases or external services - that's where mocking comes in handy. A typical Spring test setup with mocking involves two main components: Mockito - the brilliant mocking framework that Spring plays nicely with Spring Test context - Spring's testing support that lets us write integration tests Terminology: What is @Mock? @Mock creates a mock implementation for the interfaces or classes. It's like having a stunt double for you...