2017-01-18 2 views
1

Я новичок в JUnit и Spring. У меня есть фасоль весны, где я не мог писать тестовые примеры. Но это нормально с уровнем сервиса (бизнес), базы данных (Hibernate). может кто-нибудь помочь мне написать тест на метод, который у меня есть в весеннем бобах? Вот некоторые фрагменты ...Как написать тестовые кейсы Junit для 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) 
+0

Я хотел бы видеть содержимое файла "src/main/resources/config/spring/beans/DataSourceTest.xml", "файл: src/main/resources/com/dynaprice/customer/spring/CustomerBean. XML». –

+0

Только что отредактировал добавленный –

ответ

1

В вашем customerbean.xml добавить RepricingBean определения и впрыснуть BO боб к нему:

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

Попробуйте.

+0

Спасибо, что он сработал! –