title: Injecting @Autowired private field during testing
body: <p>I have a component setup that is essentially a launcher for an application. It is configured like so:</p>
  
  <pre><code>@Component
  public class MyLauncher {
      @Autowired
      MyService myService;
  
      //other methods
  }
  </code></pre>
  
  <p>MyService is annotated with the <code>@Service</code> Spring annotation and is autowired into my launcher class without any issues. </p>
  
  <p>I would like to write some jUnit test cases for MyLauncher, to do so I started a class like this:</p>
  
  <pre><code>public class MyLauncherTest
      private MyLauncher myLauncher = new MyLauncher();
  
      @Test
      public void someTest() {
  
      }
  }
  </code></pre>
  
  <p>Can I create a Mock object for MyService and inject it into myLauncher in my test class? I currently don't have a getter or setter in myLauncher as Spring is handling the autowiring. If possible, I'd like to not have to add getters and setters. Can I tell the test case to inject a mock object into the autowired variable using an <code>@Before</code> init method?</p>
  
  <p>If I'm going about this completely wrong, feel free to say that. I'm still new to this. My main goal is to just have some Java code or annotation that puts a mock object in that <code>@Autowired</code> variable without me having to write a setter method or having to use an <code>applicationContext-test.xml</code> file. I would much rather maintain everything for the test cases in the <code>.java</code> file instead of having to maintain a separate application content just for my tests.</p>
  
  <p>I am hoping to use <strong>Mockito</strong> for the mock objects. In the past I have done this by using <code>org.mockito.Mockito</code> and creating my objects with <code>Mockito.mock(MyClass.class)</code>.</p>