java testwhileidle_在Spring中整合JUnit单元测试

一 简介

在Java Web开发中,通常我们会开发很多的功能代码。在代码正式使用之前,为了确保代码能够正确实现我们预期的功能,最好是添加一些简单代码对代码逻辑进行测试。很显然,JUnit就是一个不错的单元测试工具,同时在Spring中我们也可以很方便地引入JUnit进行测试

二 代码实例

(1)引入必需的jar包:

这里除了Spring以及其他模块所需要的jar包之外,还需要引入:spring-test-4.2.3.RELEASE.jar

junit-4.10.jar

注:jar包版本使用最新稳定版即可

(2)测试项目目录结构以及配置:

bf75864d013860ca7e1e75e964b8c055.png

上面图中的一些目录是我自己新建的,为的就是将不同功能的文件分隔开。这个Demo项目采用的技术是:Spring + Spring MVC + Mybatis + MySQL + Druid连接池

context.xml文件是一些基本配置;springmvc-servlet.xml文件是 Spring MVC相关的配置;sql-map-config.xml文件是Mybatis相关配置。下面我粘贴下web.xml文件和context.xml文件的代码供大家参考,其他的一些配置文件跟这里关系不大就不粘贴出来了

i)web.xml:

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

contextConfigLocation

classpath:context/context.xml

org.springframework.web.context.ContextLoaderListener

org.springframework.web.context.request.RequestContextListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:context/springmvc-servlet.xml

1

springmvc

*.html

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

DruidStatView

/druid/*

druidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

/public/*,*.js,*.css,/druid*,*.jsp,*.swf

principalSessionName

sessionInfo

profileEnable

true

druidWebStatFilter

/*

ii)context.xml:<?xml  version="1.0" encoding="UTF-8"?>

xmlns:cache="http://www.springframework.org/schema/cache" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jee="http://www.springframework.org/schema/jee"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

destroy-method="close">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

注:jdbc.properties文件:url: jdbc:mysql://localhost:3306/cookie_db

driverClassName: com.mysql.jdbc.Driver

username: root

password: root

filters: stat,wall

maxActive: 100

initialSize: 10

maxWait: 60000

minIdle: 10

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: SELECT 123

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

maxOpenPreparedStatements: 20

removeAbandoned: true

removeAbandonedTimeout: 1800

logAbandoned: true

(3)新建测试的DAO层的代码:

由于我这里使用的是Mybatis,因此就直接使用“mybatis-generator”插件自动生成一些基本文件了

注:关于“mybatis-generator”插件的使用想了解更多可以参考我的这篇文章:https://www.zifangsky.cn/431.html

(4)单元测试示例:

在src/test/java目录下新建TestUserTable.java,其内容如下:package cn.zifangsky.test.base;

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.SpringJUnit4Cla***unner;

import cn.zifangsky.mapper.UserMapper;

import cn.zifangsky.model.User;

import junit.framework.Assert;

@RunWith(SpringJUnit4Cla***unner.class)

@ContextConfiguration(locations={"classpath:/context/context.xml"})

public class TestUserTable {

@Autowired

UserMapper userMapper;

@Test

public void testSelectByPrimaryKey(){

User user = userMapper.selectByPrimaryKey(1);

//System.out.println(user.getName());

Assert.assertEquals("admin", user.getName());  //预期值-实际值

}

}

关于这里的代码我简单解释下:@RunWith注解配置了此次测试使用的环境

@ContextConfiguration注解配置了基本的Spring配置文件的路径

UserMapper 是一个具体的DAO层的类,使用@Autowired注解自动注入到这个单元测试中

@Test注解标注的方法被当做一个测试方法,里面的内容随意。当然,这里仅仅只是测试了根据主键查询一个实体

junit.framework.Assert 这个类可以用于断言,这里就是判断从数据库中查出来的用户名是否为“admin” 。如果是,那么此次测试成功,如果不是则测试失败。如果不习惯这种写法的话还可以使用我注释掉的那样直接在控制台中打印一些数据,然后我们再手动判断

(5)运行单元测试:

关于单元测试,可以有以下几种方式来运行测试,分别是:在标注了@Test注解的方法上鼠标右键选择:Run As –> JUnit Test ,这种方式测试的就是这一个方法

在一个单元测试的Java类上鼠标右键选择JUnit单元测试,这种方式测试的是这个类中的所有标有@Test注解的方法

在一个包或者一个目录上选择JUnit单元测试。很显然,这种方式的测试的实例更多

如果一个方法测试成功,那么在JUnit视图中是这样的:

8a133c346b6f1dbe0d1cc8a0a6888cf3.png

相反,测试失败显示的视图是这样的:

cc10891c6d83c04a5adc750d43bfbd38.png

PS:上面图片中的水印是我个人博客的域名,因此还请管理员手下留情不要给我标为“转载文章”,谢谢!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值