我正在尝试将Dao的服务和服务自动装入Junit测试类,我得到了以下错误。
productService:null
java.lang.NullPointerException
at com.localbazaar.product.test.ProductServiceTest.test01_selectAuthor(ProductServiceTest.java:28)
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:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
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:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
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:236)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
当我为ProductService
编写接口和实现时,服务对象正在打印,但作者对象将变为空。
任何人都可以告诉我如何解决这个问题吗?
//DAO
package com.dao;
import com.domain.Author;
public interface AuthorMapper {
int deleteByPrimaryKey(Integer authorId);
}
//service
package com.service;
@Service
public class ProductService{
@Autowired
private AuthorMapper authorMapper;
public AuthorselectAuthorByPrimaryKey(Integer authorId) {
Author author = null;
try{
author = authorMapper.selectAuthorByPrimaryKey(authorId);
}catch(Exception e){}
return author ;
}
}
---------------------------------------------------------------------------------
// JUnit Test Class
@ContextConfiguration(locations = {"classpath:springTest/springcontext.xml"})
@Controller
public class ProductServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private ProductService productService;
@Test
public void test01_selectAuthor(){
try{
System.out.println("productService:"+productService);
Author author = productService.selectAuthorByPrimaryKey(1);
System.out.println(author);
System.out.println(author.getAuthorId());
System.out.println(author.getSourceId());
System.out.println(author.getName());
System.out.println(author.getPersonalName());
System.out.println(author.getRevision());
System.out.println(author.getLastModified());
}catch(Exception e){
e.printStackTrace();
}
}
}
----------------------------------------------------------------------------------
AuthoerMapper.xml
<resultMap id="BaseResultMap" type="com.domain.Author" >
<id column="author_id" property="authorId" jdbcType="INTEGER" />
<result column="source_id" property="sourceId" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="personal_name" property="personalName" jdbcType="VARCHAR" />
<result column="revision" property="revision" jdbcType="INTEGER" />
<result column="last_modified" property="lastModified" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
author_id, source_id, name, personal_name, revision, last_modified
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from author_t
where author_id = #{authorId,jdbcType=INTEGER}
</select>
<select id="selectAuthorBySourceId" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from author_t
where source_id = #{sourceId,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from author_t
where author_id = #{authorId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.domain.Author" >
<selectKey resultType="java.lang.Integer" keyProperty="authorId" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into author_t (source_id, name, personal_name,
revision, last_modified)
values (#{sourceId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{personalName,jdbcType=VARCHAR},
#{revision,jdbcType=INTEGER}, #{lastModified,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.domain.Author" >
<selectKey resultType="java.lang.Integer" keyProperty="authorId" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into author_t
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="sourceId != null" >
source_id,
</if>
<if test="name != null" >
name,
</if>
<if test="personalName != null" >
personal_name,
</if>
<if test="revision != null" >
revision,
</if>
<if test="lastModified != null" >
last_modified,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="sourceId != null" >
#{sourceId,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="personalName != null" >
#{personalName,jdbcType=VARCHAR},
</if>
<if test="revision != null" >
#{revision,jdbcType=INTEGER},
</if>
<if test="lastModified != null" >
#{lastModified,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.domain.Author" >
update author_t
<set >
<if test="sourceId != null" >
source_id = #{sourceId,jdbcType=VARCHAR},
</if>
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="personalName != null" >
personal_name = #{personalName,jdbcType=VARCHAR},
</if>
<if test="revision != null" >
revision = #{revision,jdbcType=INTEGER},
</if>
<if test="lastModified != null" >
last_modified = #{lastModified,jdbcType=VARCHAR},
</if>
</set>
where author_id = #{authorId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.domain.Author" >
update author_t
set source_id = #{sourceId,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
personal_name = #{personalName,jdbcType=VARCHAR},
revision = #{revision,jdbcType=INTEGER},
last_modified = #{lastModified,jdbcType=VARCHAR}
where author_id = #{authorId,jdbcType=INTEGER}
</update>
----------------------------------------------------------------------------------
//springcontext.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:springTest/config.properties</value>
</property>
</bean>
<!-- define the mysql dataSource -->
<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>
<!-- transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config>
<aop:advisor pointcut="execution(* com.service..*.*(..))" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- enable component scanning and autowire (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="com.service" />
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.domain" />
<property name="mapperLocations" value="classpath*:com/dao/*.xml" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao" />
</bean>
<bean id="productService" class="com.service.ProductService" />