title: How to write JUnit for Adapter without overcomplicated code?
body: <p>I have an adapter from <code>I1</code> to <code>ILogger</code> implemented like this:</p>
  
  <pre><code>class BAdapter() implements I1
  {
     void logA() { // nothing }
     void logB() { new BLogger().log() }
     void logC() { // nothing }
  }
  </code></pre>
  
  <p>I would like to write JUnit test, that verify the functionality, but I found it a bit problematic, since I cannot inject my Mock object instead of <code>BLogger</code>, or verify return value. I found several possible solution, but I am not sure, which is the best.</p>
  
  <p><strong>Case One:</strong>
  Add <code>void setLogger(Logger l)</code> to the <code>BAdapter</code> class.</p>
  
  <pre><code>class BAdapter() implements I1
  {
     private Logger logger = new BLogger();
  
     public void logB() { logger.log() }
     public void setLogger(Logger l) { logger = l }
     .. //rest of methods
  }
  </code></pre>
  
  <p>Cons: Why to add setter which is never used in "real", non-testing code?</p>
  
  <p><strong>Case Two:</strong>
  Add protected factory method and sublcass <code>BAdapter</code> in test package.</p>
  
  <pre><code>class BAdapter() implements I1
  {
     public void logB() { createLogger().log() }
     protected Logger createLogger() { retrun new BLogger() }
     .. //rest of methods
  }
  
  class BAdapterForTesting extends BAdapter()
  {
     protected Logger createLogger() { retrun new MockBLogger() }
  }
  </code></pre>
  
  <p>Cons: I am not sure, if this is clean and elegant solution, but I don't see much cons here.</p>
  
  <p><strong>Case Three:</strong>
  Use Abstract Factory pattern.</p>
  
  <pre><code>class BAdapter() implements I1
  {
     public void logB() { AbstractFactory.getFactory().getBLogger().log() }
     .. //rest of methods
  }
  </code></pre>
  
  <p>And somewhere in tests:</p>
  
  <pre><code>AbstractFactory.setFactory(new MockLoggersFactory())
  </code></pre>
  
  <p>Cons: This is too complex, isn't it?</p>
  
  <p><strong>Case Four:</strong> 
  Return Boolean, for example, when logging was performed. E.g.</p>
  
  <pre><code>class BAdapter() implements I1
  {
     Boolean logA() { return false; }
     Boolean logB() { return new BLogger().log() }
     Boolean logC() { return false; }
  }
  </code></pre>
  
  <p>Cons: This is kind of wourkaround. Why to return some value, when nobody needs it in "real", non-testing code?</p>
  
  <p><strong>Better Solution?</strong>
  Is there anything better?</p>