gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:40:57 24 4
gpt4 key购买 nike

我是 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)

最佳答案

在 customerbean.xml 中添加 RepricingBean 定义并向其中注入(inject) 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;

尝试一下。

关于java - 如何为 Spring 托管 bean 编写 Junit 测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41716766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com