title: Mocking a Private Variable that is Assumed to Exist
body: <p>How can you get a mock object in at runtime when it is not created/initialized in the class you are testing, it is not static (singleton pattern), or you don't have some sort of test constructor to hook into?</p>
  
  <p>In a class that I am writing some unit testing for, I have come across a scenario I haven't encountered/solved yet.  I have a JMS resource (a <code>QueueConnectionFactory</code> for reference, but it shouldn't matter), that is a private variable of the class I am testing.  Since it has the <code>javax.annotation.Resource</code> annotation, at runtime it is assumed to be available.  During testing, it is not, which creates the need for mocking this object. </p>
  
  <p>It is not a static class and is not being used in a static way, if it was I could easily mock using the various static mocking methods I have run into.  Since the resource is never created locally (in a constructor or even in a test constructor), I have no way of passing in a Mock object so that at runtime of the test, the mock is used instead of the actual object.  How can I mock this Resource so that when the test executes, it will be used in place of the private <code>@Resource</code> object in the class I am testing?</p>
  
  <p>For reference, the code is calling <code>createConnection()</code> on the <code>QueueConnectionFactory</code> which is throwing a null pointer exception since the Factory has not been initialized/mocked.</p>
  
  <pre><code>@Stateless
  public class Example{
    @Resource(name = "jms/exampleQCF")
    private QueueConnectionFactory queueFactory;
  
    ...
  
    public void testMe(){
      Connection connection = queueFactory.createConnection();
      ...
    }
  }
  </code></pre>