title: Instantiating objects when using Spring, for testing vs production
body: <p>Am correct in understanding that when using Spring, you should use the Spring configuration xml to instantiate your objects for production, and directly instantiate objects when testing? </p>
  
  <p>Eg. </p>
  
  <p><strong>MyMain.java</strong></p>
  
  <pre><code>package org.world.hello;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MyMain {
  
      private Room room;
  
  
      public static void speak(String str)
      {
          System.out.println(str);
      }
  
      public static void main(String[] args) {
  
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
          Room room = (Room) context.getBean("myRoom");
  
          speak(room.generatePoem());
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>Room.java</strong></p>
  
  <pre><code>package org.world.hello;
  
  public class Room {
  
      private BottleCounter bottleCounter;
      private int numBottles;
  
      public String generatePoem()
      {
          String str = "";
          for (int i = numBottles; i&gt;=0; i--)
          {
              str = str +  bottleCounter.countBottle(i) + "\n";
  
          }
          return str;
      }
  
      public BottleCounter getBottleCounter() {
          return bottleCounter;
      }
  
      public void setBottleCounter(BottleCounter bottleCounter) {
          this.bottleCounter = bottleCounter;
      }
  
      public int getNumBottles() {
          return numBottles;
      }
  
      public void setNumBottles(int numBottles) {
          this.numBottles = numBottles;
      }
  
  }
  </code></pre>
  
  <p><strong>BottleCounter.java</strong></p>
  
  <pre><code>package org.world.hello;
  
  public class BottleCounter {
  
      public String countBottle(int i)
      {
          return i + " bottles of beer on the wall" + i + " bottles of beer!";
      }
  
  }
  </code></pre>
  
  <p><strong>Beans.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
     &lt;bean id="myRoom" class="org.world.hello.Room"&gt;
         &lt;property name="bottleCounter"&gt;
              &lt;bean id = "myBottleCounter" class = "org.world.hello.BottleCounter"/&gt;     
         &lt;/property&gt;
         &lt;property name = "numBottles" value = "10"&gt;&lt;/property&gt;
  
     &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Outputs: (my apologies for the missing space)</p>
  
  <pre><code>10 bottles of beer on the wall10 bottles of beer!
  9 bottles of beer on the wall9 bottles of beer!
  8 bottles of beer on the wall8 bottles of beer!
  7 bottles of beer on the wall7 bottles of beer!
  6 bottles of beer on the wall6 bottles of beer!
  5 bottles of beer on the wall5 bottles of beer!
  4 bottles of beer on the wall4 bottles of beer!
  3 bottles of beer on the wall3 bottles of beer!
  2 bottles of beer on the wall2 bottles of beer!
  1 bottles of beer on the wall1 bottles of beer!
  0 bottles of beer on the wall0 bottles of beer!
  </code></pre>
  
  <p>Now for testing this:</p>
  
  <p><strong>BottleCounterTest.java</strong>:</p>
  
  <pre><code>package org.world.hello;
  
  import static org.junit.Assert.*;
  
  import org.junit.Test;
  
  public class BottleCounterTest {
  
      @Test
      public void testOneBottle() {
          BottleCounter b = new BottleCounter();
          assertEquals("1 bottles of beer on the wall1 bottles of beer!", b.countBottle(1));
      }
  
  }
  </code></pre>
  
  <p>Pretty straight forward.</p>
  
  <p><strong>RoomTest.java:</strong></p>
  
  <pre><code>package org.world.hello;
  
  import static org.junit.Assert.*;
  import org.mockito.Mockito;
  import org.junit.Test;
  
  public class RoomTest {
  
      @Test
      public void testThreeBottlesAreSeperatedByNewLines()
      {
          Room r = new Room();
          BottleCounter b = Mockito.mock(BottleCounter.class);
          Mockito.when(b.countBottle(Mockito.anyInt())).thenReturn("a");
          r.setBottleCounter(b);
          r.setNumBottles(3);
          assertEquals("a\na\na\na\n", r.generatePoem());
      }
  
  }
  </code></pre>
  
  <p>Am I correct in instantiating my test objects this way?</p>