title: How should variance in mock objects be handled?
body: <p>Let's say I have the following interface I want to mock:</p>
  
  <p><strong>Searcher.java</strong></p>
  
  <pre><code>public interface Searcher {
  
      public String search();
  
      public void someMethod();
  
  }
  </code></pre>
  
  <p>What do I do when I want to use different mock implementations for this interface? For example, in one test I would like to let the <code>search()</code> method return the empty string, in another test I would like it to start doing some HTTP requests etc. </p>
  
  <p>Do I encapsulate the behaviour, like fx. putting it in a SearchBehaviour interface and then writing implementations for that interface:</p>
  
  <pre><code>public class SearcherMock implements Searcher {
  
      private SearchBehaviour searchBehaviour;
  
      public SearcherMock(SearchBehaviour searchBehaviour) {
          this.searchBehaviour = searchBehaviour;
      }
  
      @Override
      public String search() {
          return searchBehaviour.search();
      }
  
      @Override
      public void someMethod() {
          // Do something here
      }
  
  } 
  </code></pre>
  
  <p>Or do I create a new mock class for each mock implementation that varies? Fx. <code>EmptySearcher</code> and <code>HTTPSearcher</code>?</p>