0


Spring 单元测试

1、pom文件引入依赖

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.test4j</groupId><artifactId>test4j.core</artifactId><version>2.0.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.5.RELEASE</version></dependency>

2.1、Spring单元测试基础类AbstractTransactionalTests.java

packagecom.fast.base;importorg.springframework.test.annotation.DirtiesContext;importorg.springframework.test.annotation.Rollback;importorg.springframework.test.context.ActiveProfiles;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;importorg.test4j.module.ICore;@DirtiesContext@ContextConfiguration(locations ={"classpath*:/spring/applicationContext-test-core.xml","classpath*:/spring/applicationContext-test-jdbc.xml"})//@ContextConfiguration({ "classpath:spring/applicationContext-test-*.xml" })@ActiveProfiles(profiles ="test")@Rollback(true)//默认为truepublicabstractclassAbstractTransactionalTestsextendsAbstractTransactionalJUnit4SpringContextTestsimplementsICore{}

2.2、单元测试类 DemoServiceTest.java

packagecom.fast.service;importstaticorg.junit.Assert.assertNotNull;importstaticorg.junit.Assert.assertNull;importstaticorg.junit.Assert.assertTrue;importjava.util.HashMap;importjava.util.Map;importorg.junit.Test;importorg.springframework.beans.factory.annotation.Autowired;importcom.fast.base.AbstractTransactionalTests;publicclassDemoServiceTestextendsAbstractTransactionalTests{@AutowiredDemoService demoService;@TestpublicvoidtestCreate(){String id="111";Map<String,Object> map =newHashMap<String,Object>();
        map.put("empId", id);
        map.put("leadId","1");
        map.put("empName","test");
        map.put("salary","100");
        map.put("deptNo","1");
        demoService.create(map);Map<String,Object> result = demoService.get(id);assertTrue(result !=null&& result.size()>0);}@TestpublicvoidtestDelete(){String id ="3";assertNotNull(demoService.get(id));
        demoService.delete(id);assertNull(demoService.get(id));}}

3.1、applicationContext-test-jdbc.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd"><!-- 读取属性文件 --><beanid="config"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="location"value="classpath:jdbc_test.properties"></property></bean><!-- 配置数据源 --><beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close"><propertyname="driverClassName"value="${driverClassName}"/><!-- 基本属性 url、user、password --><propertyname="url"value="${jdbc_url}"/><propertyname="username"value="${jdbc_username}"/><propertyname="password"value="${jdbc_password}"/><!-- 配置初始化大小、最小、最大 --><propertyname="initialSize"value="10"/><propertyname="minIdle"value="10"/><propertyname="maxActive"value="200"/><!-- 配置获取连接等待超时的时间 --><propertyname="maxWait"value="60000"/><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><propertyname="timeBetweenEvictionRunsMillis"value="5000"/><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><propertyname="minEvictableIdleTimeMillis"value="300000"/><propertyname="validationQuery"value="SELECT 'x' FROM DUAL"/><propertyname="testWhileIdle"value="true"/><propertyname="testOnBorrow"value="false"/><propertyname="testOnReturn"value="false"/><!-- 超过时间限制是否回收 --><propertyname="removeAbandoned"value="true"/><!-- 超时时间;单位为秒。180秒=3分钟 --><propertyname="removeAbandonedTimeout"value="180"/><!-- 关闭abanded连接时输出错误日志 --><propertyname="logAbandoned"value="true"/><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><propertyname="poolPreparedStatements"value="true"/><propertyname="maxPoolPreparedStatementPerConnectionSize"value="20"/><!-- 配置监控统计拦截的filters --><propertyname="filters"value="stat"/></bean></beans>

3.2、applicationContext-test-core.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd"><!-- 扫描manager 引入注解 --><context:component-scanbase-package="com.fast"/><!-- 启用对事务注解的支持 --><tx:annotation-driventransaction-manager="transactionManager"/><beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><propertyname="dataSource"ref="dataSource"></property></bean><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"></property></bean><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="create*"rollback-for="Exception"propagation="REQUIRED"/><tx:methodname="add*"rollback-for="Exception"propagation="REQUIRED"/><tx:methodname="save*"rollback-for="Exception"propagation="REQUIRED"/><tx:methodname="insert*"rollback-for="Exception"propagation="REQUIRED"/><tx:methodname="delete*"rollback-for="Exception"propagation="REQUIRED"/><tx:methodname="update*"rollback-for="Exception"propagation="REQUIRED"/><tx:methodname="find*"propagation="SUPPORTS"read-only="true"/><tx:methodname="get*"propagation="SUPPORTS"read-only="true"/><tx:methodname="query*"propagation="SUPPORTS"read-only="true"/><tx:methodname="select*"propagation="SUPPORTS"read-only="true"/></tx:attributes></tx:advice><aop:config><aop:advisoradvice-ref="txAdvice"pointcut="execution(* com.fast..*Service*.*(..))"/><aop:advisoradvice-ref="txAdvice"pointcut="execution(* com.fast..*manager*.*(..))"/></aop:config></beans>

3.3、jdbc_test.properties

driverClassName=oracle.jdbc.driver.OracleDriver
jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc_username=TEST
jdbc_password=TEST

4、项目结构
项目结构

参考:
java spring 单元测试_使用 Spring 进行单元测试
https://blog.csdn.net/weixin_34649105/article/details/114207434


本文转载自: https://blog.csdn.net/zhouxzcsdn/article/details/126158163
版权归原作者 一点也不想取名 所有, 如有侵权,请联系我们删除。

“Spring 单元测试”的评论:

还没有评论