title: How to deal with side effects of mocked method with JUnit/Mockito
body: <p>This might be a dumb question or something obvious to figure out, but nevertheless, I'm really struggling to solve this.
  Let's say we have the following logic within a method under test:  </p>
  
  <pre><code>@Service
  public class ServiceToTest() {
  
      @Autowired
      private SomeService someService;
  
      public String testMethod() {
          Map&lt;String, String&gt; parameters = new HashMap&lt;String, String&gt;();
          // eventParameters will be populated inside someService
          String result = someService.doLogic(parameters);
          // do something with the result here that doesn't really matter for this example
          String name = parameters.get("name").toLowerCase();
          return name;
      }
  }
  </code></pre>
  
  <p>Inside <code>SomeService</code> the parameters map is populated with some values, like the "name" in this example. I would like to mock this service in my unit test.</p>
  
  <p>Consider the following unit test snippet:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  public class ServiceToTestTest {
  
      @TestConfiguration
      static class ServiceToTestConfiguration {
          @Bean
          public ServiceToTest serviceToTest() {
              return new ServiceToTest();
      }
  
      @Autowired
      private ServiceToTest serviceToTest;
  
      @MockBean
      private SomeService someService;
  
      @Test
      public void testShouldReturnJimmy() {
          given(someService.doLogic(Mockito.anyMap())).willReturn("whatever");
          String name = serviceToTest.testMethod();
          assertThat(name).isEqualTo("jimmy");
      }
  }
  </code></pre>
  
  <p>When I execute this test I get a NullPointerException on this line:
  <code>String name = parameters.get("name").toLowerCase();</code>, which makes sense as the method that should populate this map is mocked and <code>parameters.get("name")</code> is null. Let's also assume that I really want to have a String returned from <code>doLogic(parameters)</code>, so it cannot be the parameters map.</p>
  
  <p>Is there a way to somehow instruct the mock object to populate the parameters map, or to mock the map object itself?<br>
  (The code examples here were written for this post on the fly, so please forgive me if there are any stupid mistakes that I haven't noticed while writing them ;-) )</p>