title: How to access Spring @Service object from jUnit test
body: <p><strong>Situation</strong>: I have service implementation class annotated with @Service with access to properties file.</p>
  
  <pre><code>@Service("myService")
  public class MySystemServiceImpl implements SystemService{
  
        @Resource
        private Properties appProperties;
  
  }
  </code></pre>
  
  <p>Properties object is configured through config-file. <strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;util:properties id="appProperties" location="classpath:application.properties"/&gt;
  </code></pre>
  
  <p>I want to test some methods of this implementation.</p>
  
  <p><strong>Question</strong>:How to access MySystemServiceImpl-object from test class in such way that Properties appProperties will be initialized properly?</p>
  
  <pre><code>public class MySystemServiceImplTest {
  
      //HOW TO INITIALIZE PROPERLY THROUGH SPRING? 
      MySystemServiceImpl testSubject;
  
      @Test
      public void methodToTest(){
          Assert.assertNotNull(testSubject.methodToTest());
      }     
  
  }
  </code></pre>
  
  <p>I can't simple create new MySystemServiceImpl - than methods that use appProperties throws NullPointerException. And I can't directly inject properties in the object - there is no appropriate setter-method.</p>
  
  <p>Just put correct steps here (thanks to @NimChimpsky for answer):</p>
  
  <ol>
  <li><p>I copied <strong>application.properties</strong> under test/resources dir.</p></li>
  <li><p>I copied <strong>applicationContext.xml</strong> under test/resources dir. In application context I add new bean (definition of application properties is already here):</p>
  
  <pre><code>&lt;bean id="testSubject" class="com.package.MySystemServiceImpl"&gt;
  </code></pre></li>
  <li><p>I modified test class in such way:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"/applicationContext.xml"})
  public class MySystemServiceImplTest {
  
     @Autowired
     MySystemServiceImpl testSubject;
  
  }
  </code></pre></li>
  <li><p>And this make the trick - now in my test class fully functional object is available</p></li>
  </ol>