单元测试类
路径:src\test\java
基类
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* 说明: 单元测试基础类
*
* @author liushengnan
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/conf/*.xml")
public class BaseTest {
}
测试类示例
/**
* 说明: 注释范例
*
* @author liushengnan
*
*/
public class ServiceProxyTest extends BaseTest {
@Autowired
private TaobaoService aTaobaoService;
@Autowired
private AccessRulesServiceImpl aAccessRulesServiceImpl;
@Autowired
private TongXunLuService aTongXunLuService;
@Autowired
private ZhangDanService aZhangDanService;
@Autowired
private JieBaProxyService aJieBaProxyService;
@Test
public void testQueryAsset() throws Exception {
System.err.println("11111111111111" + aTaobaoService);
Map<String, Object> dataMap = null;
MessageObject message = new MessageObject();
// 借吧
message.getBody().put("certid", "533323200203182355");
message.getBody().put("orderDate", "20181212");
message.getBody().put("income", "2000");
message.getBody().put("orderNo", "20170503140102108JB");
message.getBody().put("orderDate", "20170612");
message.getBody().put("phoneType1", "17621851303");
message.getBody().put("phoneType2", "18758013068");
message.getBody().put("certid", "421202199206110015");
message.getBody().put("orderDate", "20181212");
dataMap = aJieBaProxyService.execTask(message);
System.err.println("message:" + JsonUtils.of().toJson(message));
System.err.println("dataMap:" + JsonUtils.of().toJson(dataMap));
}
}
配置文件
路径:src\test\resources
先说明一下:
1.这些配置文件,都不是自己另外单独写的,都是从工程里的 resources 目录下 copy 过来的,有些配置文件是不需要的就精简了一下,基本是和 resources 保持一致的
2.applicationContext.xml 里,有配置数据源的地方,里面是 ${jdbc.driverClassName} 来取值 application-devdb.properties 里的配置信息。如果 copy 的过程中,路径发生变化,需要更改 applicationContext.xml 里的一段代码,参考:
<!-- 配置文件加载 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/conf/application*.properties" />
3.需要引入 spring-test 的依赖包
application-devdb.properties 的内容
#============================#
#===== Database sttings =====#
#============================#
#mysql database setting
# dev
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.11.32:3316/credit_platform?useUnicode=true&characterEncoding=utf-8
jdbc.username=hxxc_sa
jdbc.password=hxxc_sa
credit.url=http://127.0.0.1:8080/huaxia-creditcenter/msgProcess/acceptJsonReq.do
# sit
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://192.168.11.95:3306/credit_platform?useUnicode=true&characterEncoding=utf-8
#jdbc.username=hxxc_credit_conn
#jdbc.password=RQBvRKB0
#
#credit.url=http://127.0.0.1:8080/creditcenter/msgProcess/acceptJsonReq.do
# common properties
jdbc.pool.maxIdle=5
jdbc.pool.maxActive=100
jdbc.pool.maxWait =60000
applicationContext.xml 的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd"
default-lazy-init="false">
<!-- 配置文件加载 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/conf/application*.properties" />
<!-- -->
<bean class="com.huaxia.middleware.sdk.utils.SpringBeanUtils"></bean>
<!-- in18资源文件配置 -->
<bean name="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:resource</value>
<value>classpath:exception</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="cacheSeconds" value="60" />
<property name="useCodeAsDefaultMessage" value="false" />
<property name="fallbackToSystemLocale" value="false" />
</bean>
<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
<context:component-scan base-package="com.huaxia">
<!-- base-package 如果多个,用“,”分隔 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 数据源配置, 使用druid连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 测试环境地址 -->
<!--
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.11.95:3306/credit_platform" />
<property name="username" value="hxxc_credit_conn" />
<property name="password" value="RQBvRKB0" /> -->
<!-- 开发环境地址 -->
<!-- 开发环境地址
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.11.32:3316/credit_platform" />
<property name="username" value="hxxc_sa" />
<property name="password" value="hxxc_sa" /> -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="filters" value="mergeStat" />
<!-- 密码解密 -->
<!-- <property name="filters" value="config" />
<property name="connectionProperties" value="config.decrypt=true" /> -->
<!-- 申请连接的时候检测 -->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 检测连接 -->
<property name="validationQuery" value="select 'x'"></property>
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="5" />
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="100" />
<property name="maxWait" value="60000" />
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<!-- JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis_config/mybatis-config.xml" />
<property name="mapperLocations">
<value>classpath*:mybatis_config/*/*Mapper.xml</value>
</property>
</bean>
<!-- 自动扫描 将Mapper接口生成代理注入到Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage">
<value>
com.huaxia.*.*.dao
</value>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置 Annotation -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 事物切面配置 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="del*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txService" expression="execution (* com.huaxia.*.*.service..*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="txService"/>
</aop:config>
<bean id="stdSchedulerFactory" class="org.quartz.impl.StdSchedulerFactory">
</bean>
</beans>
servlet-context.xml 的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="servletHandlerAdapter" class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter" />
<!--自动扫描的包名
<context:component-scan base-package="com.huaxia.middleware" /> -->
<!-- Enables the Spring MVC @Controller programming model -->
<!-- 默认的注解映射的支持 -->
<!-- 默认的注解映射的支持,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageCOnverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean> -->
</mvc:message-converters>
</mvc:annotation-driven>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/static/**" location="/static/"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
<!-- 切面 -->
<aop:aspectj-autoproxy/>
</beans>
pom.xml 添加内容
<!-- 单元测试添加依赖包START -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- 单元测试添加依赖包END -->
总结
1.src\test\resources 的文件,基本都是从 src\main\resources 里 copy 过来的,做了一些修改
2.Basetest.java 类可以不做修改,需要注意引入的路径地址发生变化:
@ContextConfiguration(locations = "classpath:/conf/*.xml")
3.pom.xml 需要添加依赖包
4.如果数据库地址加载不了,需要注意 applicationContext.xml 里的配置,看看路径是不是对不上:
<!-- 配置文件加载 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/conf/application*.properties" />
5.如果 maven 工程比较复杂,注意单元测试类写的地址,最好是 resources 配置都在的那个地方,如果不是,需要注意 pom.xml 依赖包的缺失。单元测试相当于启动项目,所以很多该有的配置和依赖包都需要有。
广告
我现在开始做 美股/港股交易的代理,如果想要购买美股和港股(很多程序员心心念念的 BAT 公司的股票,都可以购买哦),请扫码,给我赚点零花钱,再附上我的微信,如果有什么问题,欢迎咨询交流,啦啦啦啦啦~
美股港股交易
图片好大大大大大…………………………美股/港股交易,请扫这个,注册后,可以下载
个人微信
无论是美股港股交易问题,还是文章有什么疑问,都可以问,啦啦啦啦啦