Why is my bean null?

I am trying to learn how to use applicationContext. My goal is to exchange real-time file storage using my unit tests. I do not want to do this explicitly, I want to do this with dependency injection.

So, as a simple test, before I get complicated, I'm just trying to get a bean from my applicationContext.xml. From what I read, this should work:

@ContextConfiguration(locations = "/applicationContext.xml")
public class ResultsListTests {

     @Resource
     CompanyResult resultBean;

     @Test
     public void shouldAddResults() {
         assertEquals(resultBean.getCompanyName(), "Microsoft");

But my Bean result is always zero. Here is my applicationContext.xml which is under WebContent / WEB-INF:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="resultBean" name="resultBean" class="com.trgr.cobalt.company.domain.CompanyResult">
        <property name="companyName">
            <value>Microsoft</value>
        </property>
     </bean>
</beans>

So why is my Bean result always null? What did I do wrong?

0
source share
1 answer

@RunWith(SpringJUnit4ClassRunner.class):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext.xml")
public class ResultsListTests {

     @Resource
     CompanyResult resultBean;

     @Test
     public void shouldAddResults() {
         assertEquals(resultBean.getCompanyName(), "Microsoft");
     }
}

BTW, WebContent/WEB-INF applicationContext.xml.

@ContextConfiguration(locations = "/applicationContext.xml"), Spring applicationContext.xml , WebContent/WEB-INF (jUnit 100% , -).

. Spring .

+1

Source: https://habr.com/ru/post/1777511/


All Articles