Junit测试maven+ssm项目的过程及出现的错误总结
2017-08-16 15:20
337 查看
junit进行单元测试通常有两种:
1. 不使用spring封装的junit,手动加载package com.puhua.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import com.puhua.model.Users; import com.puhua.service.UsersService; public class TestUsersMapper extends AbstractJUnit4SpringContextTests{ private UsersService usersService; @SuppressWarnings("resource") @Before public void before(){ ApplicationContext context = new ClassPathXmlApplicationContext(new String []{"classpath*/spring-mybatis.xml","classpath*:/spring-mvc.xml"}); System.out.println("_________________________"); usersService = (UsersService) context.getBean("usersServiceImpl"); } @Test public void testSelectByPrimaryKey(){ Long id = new Long(1); Users user = usersService.selectByPrimaryKey(id); System.out.println(user); } }
2.通过spring封装的junit实现注解测试:
package com.puhua.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import com.puhua.model.Users; import com.puhua.service.UsersService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:/spring-mybatis.xml","classpath*:/spring-mvc.xml"}) @WebAppConfiguration public class SpringTest extends AbstractJUnit4SpringContextTests { @Resource private UsersService usersService; @Test public void testSelectByPrimaryKey(){ Long id = new Long(1); Users user = usersService.selectByPrimaryKey(id); System.out.println(user.getUserName()); } }
下面重点讲通过spring注解方式进行junit测试出现的问题及解决方法:
1.基类上的三个注解一个都不能少:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:/spring-mybatis.xml","classpath*:/spring-mvc.xml"}) @WebAppConfiguration
其中@ContextConfiguration用来加载spring的配置文件,如果有多个,需要以逗号分开,也可以用通配符如:
@ContextConfiguration(locations = { "classpath*:/spring-*.xml"})
但需要重点注意的是:classpath后面的*号不能少,如果没有星号,会报如下错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.puhua.test.SpringTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.puhua.service.UsersService com.puhua.test.SpringTest.usersService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.puhua.service.UsersService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at ...
除此之外,@WebAppConfiguration的作用是:表明该类会使用web应用程序的默认根目录来载入ApplicationContext, 默认的更目录是”src/main/webapp”, 如果需要更改这个更目录可以修改该注释的value值。如果没加此注解,报错和上面一样
2.一切准备就绪,开始测试,发现又报错了,报错为
Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN
原因是缺少WEB服务运行的相关包,右键项目->build path->add Library->Server Runtime->选择你的tomcat,OK!
再次测试,可以查到数据!
相关文章推荐
- Junit测试maven+ssm项目的过程及出现的错误总结
- 自己项目中出现的错误总结:
- 安装Xen、创建Xen虚拟机、使用Xen虚拟机过程中经常出现的错误总结
- 最近在ArcGIS Engine开发中关于调用gp工具过程出现COM 组件的调用返回了错误 HRESULT E_FAIL 错误的解决方法 和 学习oracle中遇到的一些问题总结
- 项目开发过程中出现的错误二
- 安卓开发过程中,常因添加第三方jar包或添加依赖是出现如下错误,以下是本人一点解决方法总结(finished with non-zero exit value 2 )
- 安装Xen、创建Xen虚拟机、使用Xen虚拟机过程中经常出现的错误总结
- 搭建SSH项目过程出现的错误
- iOS 开发过程中常出现的一些错误总结
- CKFinder使用过程中出现的错误总结
- 项目开发过程中出现的错误一
- 安装Xen、创建Xen虚拟机、使用Xen虚拟机过程中经常出现的错误总结
- 项目开发过程中出现的错误一
- SVC 工作过程中出现的错误记录(SEO项目)
- 项目开发过程中出现的错误二
- 安装Xen、创建Xen虚拟机、使用Xen虚拟机过程中经常出现的错误总结
- (在hello,MFC的创建过程中,新建项目时所配置的属性出现错误)error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用 MSVCRTD.lib
- 从eclipse导出,Android Studio导入项目过程出现的各种问题总结
- MyEclipse+Maven创建web项目出现的错误总结
- Maven 项目出现的错误总结