My Shitty Code

Embracing the Messiness in Search of Epic Solutions

Tag: Mockito

  • Mockito: Effective Partial Mocking

    Sometimes, we may only want to partially mock an object to unit test our code. There are several ways to skin this cat, but I’ll show two straightforward approaches that allow you to obtain the cat fur. PROBLEM Let’s assume we want to test this service class:- Basically, getComputedValue() sums up getFirstValue() and getSecondValue() and […]

  • MockMvc + Mockito = Epic Tests

    Spring Framework 3.2 introduces a very elegant way to test Spring MVC controller using MockMvc. Based on the documentation, there are two ways to configure MockMvc:- The first approach will automatically load the Spring configuration and inject WebApplicationContext into the test. The second approach does not load the Spring configuration. While both options work, my […]

  • How to Unit Test Spring MVC Controller

    SCENARIO Let’s assume we have the following controller that needs to be tested:- SOLUTION 1: “Works but It Won’t Get You the Promotion” This working solution relies on:- While this solution works, but it has a few problems. This test case strictly tests the actual controller API, but it completely disregards the URI and request […]