准备工作:

eclipse本身带有junit4,可以直接build path,加入junit。

连接数据库的配置文件需要修改,之前的文件是采用properties+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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- Tomcat中已经配置了数据源的情况:JNDI方式配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 配置数据源 --><!-- 在引入了配置文件的情况下 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/postdoctors?" />
<property name="username" value="root" />
<property name="password" value="123456" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" /> <!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean> <!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:swust/edu/cn/postdoctors/mapping/*.xml" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="swust.edu.cn.postdoctors.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="swust.edu.cn.postdoctors.service.*" />
<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* swust.edu.cn.postdoctors.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config> <!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>swust.edu.cn.postdoctors.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config> </beans>

测试步骤:

1.加载junit类,spring配置文件,指明junit测试器,@Runwith

2.定义变量,controller类,mockHttpServletRequest类,mockHttpServletResponse类

3.初始化变量,@Before注解

4.实现测试方法,@Test注解,request.setResuestURI,request.setMethod,其他自己想测试的方法。

 package swust.edu.cn.postdoctors.controller;

 import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.web.servlet.ModelAndView;
import junit.framework.TestCase; @RunWith(SpringJUnit4ClassRunner.class) // 整合
@ContextConfiguration(locations={"classpath:spring-mybatis-test.xml","classpath:spring.xml","classpath:spring-mvc.xml"}) // 加载配置
public class UserControllerTest extends TestCase { private UserController userController; private MockHttpServletRequest request ;
private MockHttpServletResponse response ; //省略set get函数
@Before
public void before() throws Exception {
userController = new UserController();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
} @Test
public void testSelectUserById(){ request.setRequestURI("/postDoctors/userController/selectUserByLoginNameAndPswd");
request.setMethod("post"); try{
final ModelAndView mav = userController.selectUserByLoginNameAndPswd("smx","123" ,request, response);
ModelAndViewAssert.assertModelAttributeAvailable(mav, "map");
System.out.println(" ========= " );
}catch (Exception e){
e.printStackTrace();
}
} }

junit基础学习之-测试controller层(2)的更多相关文章

  1. junit基础学习之-测试service层(3)

    测试步骤: 在之前的文章中已经加了junit的环境,这就不需要了. 1.加载junit类,spring配置文件,指明junit测试器,@Runwith 2.定义变量,service,不可以使用spri ...

  2. Junit mockito 测试Controller层方法有Pageable异常

    1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...

  3. PowerMock+SpringMVC整合并测试Controller层方法

    PowerMock扩展自Mockito,实现了Mockito不支持的模拟形式的单元测试.PowerMock实现了对静态方法.构造函数.私有方法以及final方法的模拟支持,对静态初始化过程的移除等强大 ...

  4. SpringBoot测试Controller层

    一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  5. junit基础学习之-多线程测试(6)

    步骤: 1.定义单个TestRunner 2.重载单个TestRunner的runTest() 3.定义TestRunner数组,并添加多个TestRunner 4.MultiThreadedTest ...

  6. junit基础学习之-引用spring容器的测试(7)

    context 自动注入的文章链接:http://www.360doc.com/content/11/0815/09/2371584_140471325.shtml

  7. junit基础学习之-简介(1)

    JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1  JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...

  8. junit基础学习

    学习地址一:http://blog.csdn.net/andycpp/article/details/1327147/ 学习地址二:http://blog.csdn.net/zen99t/articl ...

  9. 使用Mock 测试 controller层

    package action; import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import ...

随机推荐

  1. shell脚本中执行sql脚本并传递参数(mysql为例)

    1.mysql脚本文件 t.sql insert into test.t values(@name,@age); exit 2.shell脚本文件 a.sh  (为方便演示,与t.sql文件放在同一目 ...

  2. D - Beautiful Graph (深搜)

    这个题深搜容易解决,结果用了广搜,动手之前还是要想清楚,然后自己的代码写错的情况下,没有重写,而是在原有的基础上,进行修改,结果有个判定的初始化条件放错位置,浪费了一个小时... 就是给一个无向图,任 ...

  3. [CISCN2019 华北赛区 Day1 Web2]ikun

    知识点:逻辑漏洞.jwt密钥破解.python反序列化漏洞 进入靶机查看源码: 提示需要买到lv6,注册账号发现给了1000块钱,根据ctf套路应该是用很低的价格买很贵的lv6,首页翻了几页都没发现l ...

  4. 《Netlogo多主体建模入门》笔记4

    4- 从Langton的蚂蚁看Turtle与Patch的交互   这只蚂蚁从10000步开始,就会自发地 “建桥”     Turtle与Patch就好比是,一个方块和一个格子的关系. 一个格子上可以 ...

  5. [7b2美化]柒比贰 魔改系列|7B2-分类封面添加波浪效果&每日诗词

    本文转载自:钻芒博客 https://www.zmki.cn/5105.html 效果如图: 代码: 首先在style.css样式表里添加波浪样式 /*浪来了*/ .lang { overflow: ...

  6. 二十一 JDK注解&注解案例

    什么是注解? 注解和接口,类属于同一个级别 注解可以在变量.方法.类上加载 注解可以有属性也可以没有属性 注解是有作用范围(源码.编译期间,运行期间) 源码期间:例如String类上@Author  ...

  7. 吴裕雄--天生自然Hadoop学习笔记:Hadoop简介

    Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.Hadoop实现了一个分布式文件系统(H ...

  8. AC自动机 (模板)

    AC自动机是用来干什么的: AC自动机是用来解决多模匹配问题,例如有单词s1,s2,s3,s4,s5,s6,问:在文本串ss中有几个单词出现过,类似. AC自动机实现这个功能需要三个部分: 1.将所有 ...

  9. zabbix agent的主动工作模式实战案例

    zabbix agent的主动工作模式实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.zabbix agent的工作模式概述 zabbix agent的主动工作模式: ...

  10. 17. Getting to the essence of things

    17.Getting to the essence of things.抓住事情的本质 From today on, I think I should keep a diary. To the CCU ...