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

Posted

技术标签:

【中文标题】如何为 Spring 托管 bean 编写 Junit 测试用例?【英文标题】:How to write Junit test cases for Spring managed bean? 【发布时间】:2017-01-18 10:21:26 【问题描述】:

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

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)

【问题讨论】:

我想查看“file:src/main/resources/config/spring/beans/DataSourceTest.xml”、“file:src/main/resources/com/dynaprice/customer/”的内容春天/CustomerBean.xml”。 刚刚编辑添加 【参考方案1】:

在您的 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;

试试看。

【讨论】:

以上是关于如何为 Spring 托管 bean 编写 Junit 测试用例?的主要内容,如果未能解决你的问题,请参考以下文章

如何为配置类编写@Bean的junit案例

如何为 SOA 有效管理/托管许多 WCF 服务

如何为spring数据编写自定义模块

如何为具有 Spring Security 配置的 Spring Boot API 编写单元测试

如何为带有注释的 spring 设置 context.xml?

如何为具有spring存储库代码的@Component类编写集成测试?