使用junit进行单元测试
创建测试类
在dataService工程下创建测试类
在java文件夹下再创建相应的package和class类。
ServiceTest测试类中的内容:
public class ServiceTest {
private IBidInfoService bidInfoService;
private ILoanInfoService loanInfoService;
private IUserService userService;
@Before
public void before(){
String conf1 = "spring-ds.xml";
String conf2 = "spring-mybatis.xml";
String conf3 = "spring-provider.xml";
String conf4 = "spring-redis.xml";
String conf5 = "spring-service.xml";
String conf6 = "spring-tx.xml";
String[] confs = {conf1, conf2, conf3, conf4, conf5, conf6};
ApplicationContext ac = new ClassPathXmlApplicationContext(confs);
bidInfoService = ac.getBean(IBidInfoService.class);
loanInfoService = ac.getBean(ILoanInfoService.class);
userService = ac.getBean(IUserService.class);
}
@Test
public void test01(){
//测试平台用户总人数
Long totalUserCount = userService.findTotalUserCount();
System.out.println(totalUserCount);
}
@Test
public void test02() {
//测试平均历史年化收益率
Double historyAvgRate = loanInfoService.findHistoryAvgRate();
System.out.println(historyAvgRate);
}
@Test
public void test03() {
//测试平台总成交额
Double totalBidAmount = bidInfoService.findTotalBidAmount();
System.out.println(totalBidAmount);
}
}
开启Redis与Zookeeper虚拟机
移动dao层对应的映射文件的位置
将原本与dao层接口在一块的映射文件,移动到resources下。这样做的原因是:不移动的话会报出,MyBatis绑定错误:Invalid bound statement (not found)。
解决方法:参考博客
指定移动后的映射文件的位置
在主配置文件mybatis.xml中添加这三个配置文件的位置。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 注册实体类别名 -->
<typeAliases>
<package name="com.yanchuanyi.p2p.exterface.bean"/>
</typeAliases>
<!-- 注册Mapper -->
<mappers>
<!--<package name="com.yanchuanyi.p2p.dataService.dao"/>-->
<mapper resource="IBidInfoDao.xml"/>
<mapper resource="ILoanInfoDao.xml"/>
<mapper resource="IUserDao.xml"/>
</mappers>
</configuration>
对test02的测试结果。
阶段性的测试成功证明,项目搭建成功,与数据库交互无碍,接下来进行进一步测试。
web工程的测试
添加Tomcat服务器
选择Deployment,点击+号,选择Artiface…
选择dataService:wer,作为这个tomcat所运行的项目。
下面的Application context的值可自行设置,设置完毕点击Apply完成tomcat01的配置。
紧接着再添加另一个tomcat02,运行web工程。
修改端口号,避免多个服务器一起运行发生冲突。
IndexHandler处理器类
@Controller
public class IndexHandler {
@Autowired
private ILoanInfoService loanInfoService;
@Autowired
private IBidInfoService bidInfoService;
@Autowired
private IUserService userService;
@RequestMapping("/index.do") //从web.xml文件中注册的中央调度器规定的*.do为后缀
public String indexHandler(Model model){
Map<String,Object> map = new HashMap<>();
map.put("pageStartIndex", 0);
//查询新手宝产品(产品类型:0,页面大小:1),并将其存放到Model中
map.put("productType",0);
map.put("pageSize",1);
List<LoanInfo> xinLoanInfos = loanInfoService.findLoanInfoByProductTypeAndPage(map);
model.addAttribute("xinLoanInfos",xinLoanInfos);
for (LoanInfo loaninfo : xinLoanInfos) {
System.out.println(loaninfo);
}
System.out.println("----------------------");
//查询优选类产品(产品类型:1,页面大小:4),并将其存放到Model中
map.put("productType",1);
map.put("pageSize",4);
List<LoanInfo> youLoanInfos = loanInfoService.findLoanInfoByProductTypeAndPage(map);
model.addAttribute("youLoanInfos",youLoanInfos);
for (LoanInfo loaninfo : youLoanInfos) {
System.out.println(loaninfo);
}
System.out.println("----------------------");
//查询散标类产品(产品类型:2,页面大小:8),并将其存放到Model中
map.put("productType",2);
map.put("pageSize",8);
List<LoanInfo> sanLoanInfos = loanInfoService.findLoanInfoByProductTypeAndPage(map);
model.addAttribute("sanLoanInfos",sanLoanInfos);
for (LoanInfo loaninfo : sanLoanInfos) {
System.out.println(loaninfo);
}
System.out.println("----------------------");
//查询平台历史年化收益率
Double historyAvgRate = loanInfoService.findHistoryAvgRate();
model.addAttribute("historyAvgRate",historyAvgRate);
System.out.println("historyAvgRate = " + historyAvgRate);
//查询平台总人数
Long totalUserCount = userService.findTotalUserCount();
model.addAttribute("totalUserCount",totalUserCount);
System.out.println("totalUserCount = " + totalUserCount);
//查询平台总成交额
Double totalBidAmount = bidInfoService.findTotalBidAmount();
model.addAttribute("totalBidAmount",totalBidAmount);
System.out.println("totalBidAmount = " + totalBidAmount);
return "/index.jsp";
}
}
这个测试的目的是tomcat服务器是否能正常使用,查询的结果在控制台上输出。
当然Redis、Zookeeper还是正常启动的。
为避免数据库中有空数据报错,所以,需要修改一下spring-ds.xml这个配置文件中的内容。将url的值进行修改。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注册数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///p2p?zeroDateTimeBehavior=convertToNull"/>
<property name="username" value="root"/>
<property name="password" value="991018"/>
</bean>
</beans>
启动tomcat
先选中tomcat01,然后点击右边的绿色启动按钮。
启动完成后,会显示dataService工程中默认的index页面。
然后再选中tomcat02,再进行启动。
启动成功后,会显示web工程中静态页面的内容。
此时控制台中会出现查询的查询的结果,就说明测试成功。