Should I use a seperate beans.xml configuration for instantiating objects in my unit tests?

心不动则不痛 提交于 2019-12-25 07:48:04

问题


Here's my Beans.xml for my application

<?xml version="1.0" encoding="UTF-8"?>

<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">


        <bean id="myRoom" class="org.world.hello.Room">
            <property name="bottleCounter">
                <bean id="myBottleCounter" class="org.world.hello.BottleCounter" />
            </property>
            <property name="numBottles" value="10"></property>

        </bean>    
</beans>

It's one Room bean that has a BottleCounter bean as a property.

Now, I want to write a unit testing for BottleCounter.

public class BottleCounterTest {

    ApplicationContext context; 

    @Before
    public void setUp()
    {
        context = new ClassPathXmlApplicationContext("Beans.xml");      
    }

    @Test
    public void testOneBottle() {

        BottleCounter bottleCounter = (BottleCounter) context.getBean("myBottleCounter");

        assertEquals("1 bottles of beer on the wall1 bottles of beer!", bottleCounter.countBottle(1));
    }

}

But I can't directly reference myBottleCounter as it's a inner bean? And gives me No bean named 'myBottleCounter' is defined.

So should instead, I be defining my test beans in a seperate xml?

eg.

testBeans.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">


        <bean id="testRoom" class="org.world.hello.Room">
            <property name="bottleCounter">
                <bean id="myBottleCounter" class="org.world.hello.BottleCounter" />
            </property>
            <property name="numBottles" value="3"></property>

        </bean>

        <bean id="testBottleCounter" class="org.world.hello.BottleCounter" />



</beans>

BottleCounterTest.java

public class BottleCounterTest {

    ApplicationContext context; 

    @Before
    public void setUp()
    {
        context = new ClassPathXmlApplicationContext("testBeans.xml");      
    }

    @Test
    public void testOneBottle() {

        BottleCounter bottleCounter = (BottleCounter) context.getBean("testBottleCounter");

        assertEquals("1 bottles of beer on the wall1 bottles of beer!", bottleCounter.countBottle(1));
    }

}

回答1:


You can do it this way:

Room room = (Room) context.getBean("myRoom");
BottleCounter bottleCounter = room.getBottleCounter();

OR, if you can modify the XML file, then declare the BottleCounter bean separately.

<bean id="testRoom" class="org.world.hello.Room">
    <property name="bottleCounter">
        <ref bean="myBottleCounter"/>
    </property>
    <property name="numBottles" value="3"></property>

</bean>

<bean id="myBottleCounter" class="org.world.hello.BottleCounter" />



回答2:


The recommended way to do this would be:

<bean id="myBottleCounter" class="org.world.hello.BottleCounter" />

<bean id="testRoom" class="org.world.hello.Room">
  <property name="bottleCounter">
    <ref bean="myBottleCounter"/>
  </property>
  <property name="numBottles" value="3"></property>
</bean>

@ContextConfiguration(locations = "classpath:Beans.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class BottleCounterTest {
  @Autowired
  BottleCounter bottleCounter;

  @Test
  public void testOneBottle() {
    assertEquals("1 bottles of beer on the wall1 bottles of beer!", bottleCounter.countBottle(1));
  }
}

To summarize, you declare an instance of the bean BottleCounter explicitly and then inject it into your test class using Spring.

Make sure that spring-test.jar is on the classpath when the tests are run.



来源:https://stackoverflow.com/questions/29224579/should-i-use-a-seperate-beans-xml-configuration-for-instantiating-objects-in-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!