错误:使用@ContextConfiguration进行单元测试时,总是报空指针异常

在做SSM项目整合时,发现在测试下列代码时,出现空指针异常错误
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
根据ssh做项目,总是报空指针不能进入userservice,调试,userservice为null
发现是spring的配置文件没有配置成功。
加上:

@RunWith(SpringJUnit4ClassRunner.class)

在这里插入图片描述
出现以下异常:
在这里插入图片描述
在pom.xml中修改依赖
在这里插入图片描述
继续出现以下错误:

[16:56:24:373] [WARN] - org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:559) - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
[16:56:24:377] [INFO] - com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1908) - {dataSource-1} closed
[16:56:24:381] [ERROR] - org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@39d9314d] to prepare test instance [cn.imu.service.UserServiceTest@604b1e1d]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
		

其中最为关键的地方就是:
在这里插入图片描述
主要原因:

[17:11:30:177] [WARN] - org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:559) - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap

解决方案:Caused by: java.lang.ClassNotFoundException: Cannot find class: userMap
在这里插入图片描述

在这里插入图片描述

java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for cn.imu.dao.UserMapper.UserMap
** BEGIN NESTED EXCEPTION ** 


Could not find parameter map Java.util.Map Parameter Maps collection does not contain value for java
发现自己使用了 parameterMap标签,因为mybatis官方已经停止支持parameterMap标签 所以 以后不要再用,改为parameterType,但是又会报最开始的错误。
真烦人,最好的办法就是,放弃使用结果集映射,重新建立实体类,使数据库中的字段和实体类中的属性一一对应。在parameterType后填入新建实体类。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值