问题描述
- spring4+mybatis3整合 junit测试的问题
- applicationContext.xml
<context:component-scan base-package=""com""/> <bean id=""propertyConfigurer"" class=""org.springframework.beans.factory.config.PropertyPlaceholderConfigurer""> <property name=""locations""> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id=""dataSource"" class=""org.apache.commons.dbcp.BasicDataSource"" destroy-method=""close""> <property name=""driverClassName"" value=""${driverClassName}""/> <property name=""url"" value=""${url}""/> <property name=""username"" value=""${username}""/> <property name=""password"" value=""${password}""/> </bean> <!-- sessionFactory 配置--> <bean id=""sqlSessionFactory"" class=""org.mybatis.spring.SqlSessionFactoryBean""> <property name=""dataSource""> <ref bean=""dataSource"" /> </property> <property name=""mapperLocations"" value=""classpath:com/zhikun/bean/basexml/*.xml""/> <property name=""configLocation"" value=""classpath:SqlMapConfig.xml""/> </bean> <bean id=""sqlSessionTemplate"" class=""org.mybatis.spring.SqlSessionTemplate"" > <constructor-arg index=""0"" ref=""sqlSessionFactory"" /> <constructor-arg index=""1"" value=""BATCH"" /> </bean> <bean id=""baseDao"" class=""com.zhikun.dao.base.BaseDaoImpl2""> <property name=""sqlSessionTemplate"" ref=""sqlSessionTemplate""></property> </bean> <tx:annotation-driven /> mybatis.xml <configuration>
basedao
public class BaseDaoImpl2 implements IBaseDao {
public SqlSessionTemplate sqlSessionTemplate;public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate;}@Overridepublic int save(T t) { return sqlSessionTemplate.insert(""com.zhikun.bean.Person.save"" t);}
Test类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={""classpath:applicationContext.xml""})
public class TestDao extends AbstractJUnit4SpringContextTests {
@Test
public void save(){
PersonDao persondao = new PersonDao();
Person p = new Person();
p.setName(""zhouhang"");
p.setNickname(""周航"");
p.setSeqce(123L);
p.setBirthday(new Date());
//sqlSessionTemplate.insert(""com.zhikun.bean.Person.save"" p);
persondao.save(p);
}
}}
解决方案
yichangcuowu
java.lang.NullPointerException
at com.zhikun.dao.base.BaseDaoImpl2.save(BaseDaoImpl2.java:25)
at com.test.zhouhang.TestDao.save(TestDao.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
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:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
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)