给项目添加spring的测试单元
2017-12-21 00:00
387 查看
1. 首先依赖两个包(junit-*.jar和spring-test-*RELEASE.jar。)尽量拿最新的即可
2. 然后建立一个测试类
3. @RunWith(SpringJUnit4ClassRunner.class) 注明测试单元
4. @ContextConfiguration(locations = {"classpath:/confg/applicationContext.xml"})
因为不是通过web.xml的启动方式去启动配置文件加载spring容器,所以这里@ContextConfiguration引入spring容器的配置文件,以启动所需的spring容器。如下就是需要启动的一个service bean 容器
5. @Autowired
private ProjectService projectService;
有了第4步的启动spring容器之后,这里方能注入。
6. applicationContext.xml容器配置文件
1. 最基本的是扫描要通过注解(@Autowired)注入的包
2. 通过spring jdbcTemplate模板对数据库进行操作
1)配置数据源
2)根据数据源准备好jdbcTemplate Bean
3) 配置需要注入jdbcTemplate的包(报下所有)
4) 两层注解注入POJO中(必须一层service加一层DAO,缺一不可,不能直接跳过service去直接调用DAO)
@service (service)
@Repository (DAO)
<!--单元测试--> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> <scope>test</scope> </dependency>
2. 然后建立一个测试类
package com.skyautobuild.test; import com.skyautobuild.service.ProjectService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:/confg/applicationContext.xml"}) public class ProjectTest { @Autowired private ProjectService projectService; @Test public void test(){ projectService.addProject(); } }
3. @RunWith(SpringJUnit4ClassRunner.class) 注明测试单元
4. @ContextConfiguration(locations = {"classpath:/confg/applicationContext.xml"})
因为不是通过web.xml的启动方式去启动配置文件加载spring容器,所以这里@ContextConfiguration引入spring容器的配置文件,以启动所需的spring容器。如下就是需要启动的一个service bean 容器
5. @Autowired
private ProjectService projectService;
有了第4步的启动spring容器之后,这里方能注入。
6. applicationContext.xml容器配置文件
1. 最基本的是扫描要通过注解(@Autowired)注入的包
<context:component-scan base-package="com.skyautobuild.service"/>
2. 通过spring jdbcTemplate模板对数据库进行操作
1)配置数据源
<context:property-placeholder location="classpath:/confg/jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/>
2)根据数据源准备好jdbcTemplate Bean
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"/>
3) 配置需要注入jdbcTemplate的包(报下所有)
<context:component-scan base-package="com.skyautobuild.dao"/>
4) 两层注解注入POJO中(必须一层service加一层DAO,缺一不可,不能直接跳过service去直接调用DAO)
@service (service)
package com.skyautobuild.service; import com.skyautobuild.dao.ProjectDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ProjectService { @Autowired private ProjectDao projectDao; public void addProject(){ projectDao.addProj(); } }
@Repository (DAO)
package com.skyautobuild.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class ProjectDao { @Autowired private JdbcTemplate jdbcTemplate; public void addProj(){ jdbcTemplate.update("insert into project(numb,text) values('weidebo','WDB')"); } }
相关文章推荐
- 最小Spring测试项目-03
- linux学习第六十二篇:添加自定义监控项目,配置邮件告警,测试告警,不发邮件的问题处理
- Spring Boot项目添加jsp支持
- idea项目添加spring
- maven构建项目二(添加配置文件)SpringMvc+Spring+Mybatis
- 19.12 添加自定义监控项目 配置邮件告警 测试告警
- 【Spring】Spring高级话题-测试Spring项目
- 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
- Gradle项目之Spring.profile.开发.测试.生产环境的配置和切换之一键部署
- linkinFrame--测试项目添加git管理
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- 最小Spring测试项目-04
- 使用junit4测试spring项目中service方法
- Visual Studio 2012 MFC项目添加原生测试用例
- IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架
- vc6中添加c++测试项目
- 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
- linkinFrame--测试项目添加git管理
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- spring和Mybatis整合项目测试