如何为Spring托管bean编写Junit测试用例?

时间:2017-01-18 10:21:26

标签: java spring junit

我是JUnit和Spring的新手。我有一个Spring bean,我无法编写测试用例。但是服务(业务),数据库(Hibernate)层很好。有人可以帮我写一个测试我在spring bean中的方法吗?这是一些片段......

Bean.java

public class RepricingBean implements Serializable {

RepricingBo repricingBo;
public RepricingBo getRepricingBo() {
    return repricingBo;
}

public void setRepricingBo(RepricingBo repricingBo) {
    this.repricingBo = repricingBo;
}

//Method to retrieve data from database Repricing & Rule tables
public List<Repricing> getRepricingRules(){
    return repricingBo.getRepricingRules();
}}

我已经编写了一些测试文件,但我无法解决我遇到的错误。

TestBean.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/resources/config/spring/beans/HibernateSessionFactory.xml",
"file:src/main/resources/config/spring/beans/DataSourceTest.xml",
"file:src/main/resources/com/dynaprice/customer/spring/CustomerBean.xml"})
public class RepricingBeanTest {

RepricingBean beanrep ;

@Test
public void testGetRepricingRules() {
    int iSize = beanrep.getRepricingRules().size();
    assertNotNull(iSize);

}

datasourcetest.xml

<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-2.5.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/dynprice" />
<property name="username" value="root" />
<property name="password" value="psk0410" />
</bean>

</beans>

customerbean.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-2.5.xsd">
<bean id="repricingBo" 
     class="com.dynaprice.customer.bo.impl.RepricingBoImpl" >
  <property name="repricingDao" ref="repricingDao" />
</bean>

<bean id="repricingDao" 
     class="com.dynaprice.customer.dao.impl.RepricingDaoImpl" >
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

错误

java.lang.NullPointerException
at com.dynaprice.RepricingBean.getRepricingRules(RepricingBean.java:364)
at com.dynaprice.beans.test.RepricingBeanTest.testGetRepricingRules(RepricingBeanTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

1 个答案:

答案 0 :(得分:2)

在您的customerbean.xml中添加RepricingBean定义并将BO bean注入其中:

<bean id="repricingBean" 
     class="yourpackage.RepricingBean" >
  <property name="repricingBo" ref="repricingBo" />
</bean>

<bean id="repricingBo" 
     class="com.dynaprice.customer.bo.impl.RepricingBoImpl" >
  <property name="repricingDao" ref="repricingDao" />
</bean>

然后在你的测试中,我会看到@Autowired注释:

public class RepricingBeanTest {

@Autowired
RepricingBean beanrep;

尝试一下。