title: Mockito, Testing an object that relies on injected dependencies (Spring)?
body: <p>I'm new to using Mockito and am trying to understand a way to make a unit test of a class that relies on injected dependencies. What I want to do is to create mock objects of the dependencies and make the class that I am testing use those instead of the regular injected dependencies that would be injected by Spring. I have been reading tutorials but am a bit confused on how to do this.</p>
  
  <p>I have one the class I want to test like this:</p>
  
  <pre><code>package org.rd.server.beans;
  
  import org.springframework.beans.factory.annotation.Autowired;
  
  public class TestBean1 {
  
      @Autowired
      private SubBean1 subBean1;
  
      private String helloString;
  
      public String testReturn () {
  
          subBean1.setSomething("its working");
          String something = subBean1.getSomething();
          helloString = "Hello...... " + something;
  
          return helloString;
      }
  </code></pre>
  
  <p>Then I have the class that I want to use as a mock object (rather than the regular <code>SubBean1</code> class, like below:</p>
  
  <pre><code>package org.rd.server.beans.mock;
  
  public class SubBean1Mock {
  
      private String something;
  
      public String getSomething() {
          return something;
      }
  
      public void setSomething(String something) {
          this.something = something;
      }
  
  
  }
  
  
      }
  </code></pre>
  
  <p>I just want to try running a simple test like this:</p>
  
  <pre><code>package test.rd.beans;
  import org.rd.server.beans.TestBean1;
  
  import junit.framework.*;
  
  
  public class TestBean1Test extends TestCase
  {
      private TestBean1 testBean1;
  
      public TestBean1Test(String name)
      {
          super(name);
      }
  
      public void setUp()
      {
          testBean1 = new TestBean1();
          // Somehow inject the mock dependency SubBean1Mock ???
  
      }
  
      public void test1() {
          assertEquals(testBean1.testReturn(),"working");
      }
  }
  </code></pre>
  
  <p>I figure there must be some fairly simple way to do this but I can't seem to understand the tutorials as I don't have the context yet to understand everything they are doing / explaining. If anyone could shed some light on this I would appreciate it.</p>