我应该使用单独的beans.xml配置来实例化单元测试中的对象吗?

时间:2015-03-24 03:33:06

标签: spring unit-testing instantiation

这是我的应用程序的Beans.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="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>

它是一个Room bean,它有一个BottleCounter bean作为属性。

现在,我想为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));
    }

}

但是我无法直接引用myBottleCounter,因为它是一个内在的bean?并且给了我No bean named 'myBottleCounter' is defined

相反,我应该在一个单独的xml中定义我的测试bean?

例如。

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));
    }

}

2 个答案:

答案 0 :(得分:0)

你可以这样做:

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

或者,如果您可以修改XML文件,则单独声明BottleCounter bean。

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

答案 1 :(得分:0)

建议的方法是:

<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));
  }
}

总而言之,您明确声明了bean BottleCounter的一个实例,然后使用Spring将它注入到您的测试类中。

运行测试时,请确保spring-test.jar位于类路径上。