Spring 3使用JUnit 4进行测试– ContextConfiguration和AbstractTransactionalJUnit4SpringContextTests...

在Internet上寻找一种测试我的Spring 3应用程序的方法,我找到了许多描述如何使用JUnit测试应用程序的文章。 它们中的大多数都是不完整的示例,实际上并不起作用。

在这篇文章中,我将尝试填补这一空白,并撰写一篇简洁而简单的文章,介绍如何使用Junit 4测试spring 3应用程序。Ta使其更易于阅读和遍历,我不会使用现在很大的应用程序但我将使用viralpatel提供的样本。 下载项目的链接是这里 。

下载此应用程序并将其导入eclipse。

在您的数据库中执行以下命令:

create database contact;
use contact;
CREATE TABLE CONTACTS
(id              INT PRIMARY KEY AUTO_INCREMENT,firstname    VARCHAR(30),lastname    VARCHAR(30),telephone   VARCHAR(15),email         VARCHAR(30),created     TIMESTAMP DEFAULT NOW()
);

在pom.xml中添加以下依赖项:

<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>1.0.1.RELEASE</version>
</dependency>
<dependency><groupId>org.junit</groupId><artifactId>com.springsource.org.junit</artifactId><version>4.7.0</version><scope>test</scope>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>org.springframework.test</artifactId><version>${org.springframework.version}</version><scope>test</scope>
</dependency>
<dependency> <groupId>javax.transaction</groupId><artifactId>com.springsource.javax.transaction</artifactId><version>1.1.0</version>
</dependency>

还为存储库添加以下内容:

<repositories><repository><id>com.springsource.repository.bundles.release</id><name>SpringSource Enterprise Bundle Repository - SpringSource Releases</name><url>http://repository.springsource.com/maven/bundles/release</url></repository><repository><id>com.springsource.repository.bundles.external</id><name>SpringSource Enterprise Bundle Repository - External Releases</name><url>http://repository.springsource.com/maven/bundles/external</url></repository><repository><id>com.springsource.repository.bundles.milestone</id><name>SpringSource Enterprise Bundle Repository - SpringSource Milestones</name><url>http://repository.springsource.com/maven/bundles/milestone</url></repository><repository><id>com.springsource.repository.bundles.snapshot</id><name>SpringSource Enterprise Bundle Repository - Snapshot Releases</name><url>http://repository.springsource.com/maven/bundles/snapshot</url></repository><repository><id>repository.springframework.maven.release</id><name>Spring Framework Maven Release Repository</name><url>http://maven.springframework.org/release</url></repository><repository><id>repository.springframework.maven.milestone</id><name>Spring Framework Maven Milestone Repository</name><url>http://maven.springframework.org/milestone</url></repository><repository><id>repository.springframework.maven.snapshot</id><name>Spring Framework Maven Snapshot Repository</name><url>http://maven.springframework.org/snapshot</url></repository><repository><id>jboss</id><name>JBoss repository</name><url>https://repository.jboss.org/nexus/content/repositories/releases</url></repository>
</repositories>

在目录src / test / java下创建以下软件包:
net.viralpatel.contact.form

在刚创建的包中,创建一个名为:
抽象接触测试

在src / test / resources下创建以下内容:
净/ viralpatel /联系人/表格

在其中创建以下文件:
AbstractContactTests-context.xml

注意! 确保目录,软件包,类和xml文件的创建位置与上述位置完全相同。 您将看到xml文件使用测试类的名称加上“ -context.xml”,并且该文件是在相同目录结构下创建的。 这很重要,因为spring会自动在指定目录下查找具有指定名称的xml文件。

现在,在AbstractContactTests类中插入以下内容:

package net.viralpatel.contact.form;import net.viralpatel.contact.dao.ContactDAO;
import net.viralpatel.contact.service.ContactService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;@ContextConfiguration
public class AbstractContactTests extends AbstractTransactionalJUnit4SpringContextTests {@Autowiredprotected ContactDAO contact;@Autowiredprotected ContactService contactService;@Testpublic void sampleTest(){System.out.println("Number of rows is: " + contactService.listContact().size());System.out.println("Creating a new contact");Contact cont = new Contact();cont.setEmail("giannis@gmail.com");cont.setLastname("ntantis");cont.setFirstname("ioannis");cont.setTelephone("00306985587996");System.out.println("Before saving contact");contactService.addContact(cont);System.out.println("After saving contact. Id if contact is: " + cont.getId());System.out.println("Number of rows now is: " + contactService.listContact().size());}
}

@ContextConfiguration注释告诉spring如何加载和配置应用程序上下文。 我们还可以告诉spring在哪里可以找到文件,例如:

@ContextConfiguration(locations = {“ example / test-context.xml”},loader = CustomContextLoader.class)

通过不提供任何参数,spring将在与类包相同的目录下查找xml文件,并在名为class.name-context.xml的文件中查找(记住上面的提示)。

请注意,我们的类从org.springframework.test.context.junit4扩展了AbstractTransactionalJUnit4SpringContextTests 。 通过扩展此类,我们在类级别为方法提供了事务支持。 如果我们不这样做,我们需要事务支持,则必须使用@Transactional注释方法,或者使用@TransactionConfiguration注释配置事务管理器。

在AbstractContactTests-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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:property-placeholder location="classpath:jdbc.properties"/><context:annotation-config/><tx:annotation-driven/><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/contact"p:username="root" p:password="123456"/><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property><property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${jdbc.dialect}</prop><prop key="hibernate.show_sql">true</prop></props></property></bean><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="contactDAO" class="net.viralpatel.contact.dao.ContactDAOImpl"></bean><bean id="contactService" class="net.viralpatel.contact.service.ContactServiceImpl"></bean>
</beans>

在这里,您将看到spring-servlet.xml中定义的许多定义。 在我的示例中,它们与spring-servlet.xml相同,但是您可以随意更改它们。 这样,Spring就有机会为测试过程创建不同的数据源,甚至为每个测试类创建不同的数据源。

现在,执行AbstractContactTests类作为junit测试。 您应该获得以下输出:

Hibernate: select contact0_.ID as ID0_, contact0_.EMAIL as EMAIL0_, contact0_.FIRSTNAME as FIRSTNAME0_, contact0_.LASTNAME as LASTNAME0_, contact0_.TELEPHONE as TELEPHONE0_ from CONTACTS contact0_
Number of rows is: 0
Creating a new contact
Before saving contact
Hibernate: insert into CONTACTS (EMAIL, FIRSTNAME, LASTNAME, TELEPHONE) values (?, ?, ?, ?)
After saving contact. Id if contact is: 2
Hibernate: select contact0_.ID as ID0_, contact0_.EMAIL as EMAIL0_, contact0_.FIRSTNAME as FIRSTNAME0_, contact0_.LASTNAME as LASTNAME0_, contact0_.TELEPHONE as TELEPHONE0_ from CONTACTS contact0_
Number of rows now is: 1

多数民众赞成在所有您需要的人。 祝您编程愉快,别忘了分享!

参考: 使用JUnit 4进行Spring 3测试。使用来自Giannisapi博客的 JCG合作伙伴 Ioannis Ntantis的@ContextConfiguration和AbstractTransactionalJUnit4SpringContextTests 。

相关文章 :
  • JUnit 4.9(测试版3)中的规则
  • 单元和集成测试的代码覆盖率
  • Spring MVC3 Hibernate CRUD示例应用程序
  • 将Jersey与Spring整合
  • Spring,Quartz和JavaMail集成教程
  • Spring声明式事务示例
  • Java教程和Android教程列表

翻译自: https://www.javacodegeeks.com/2011/10/spring-3-testing-with-junit-4.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/358038.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

RecyclerView滑动到底部自动加载

你经常听到“上拉加载”这样的字眼吗&#xff1f;你知道这个功能是怎么实现的吗&#xff1f;这篇文章记录了我对“上拉加载”的实现&#xff0c;与大家一起分享。 “上拉加载”针对的是RecyclerView或者Listview这样的列表控件&#xff08;本文以RecyclerView为例&#xff09;&…

javaScript学习笔记之typeof, null, 和 undefined之间的对比

typeof 操作符 你可以使用 typeof 操作符来检测变量的数据类型。 null 在 JavaScript 中 null 表示 "什么都没有"。 null是一个只有一个值的特殊类型。表示一个空对象引用。 undefined 在 JavaScript 中, undefined 是一个没有设置值的变量。 typeof 一个没有值的变量…

不喜欢节流吗?

您别无选择–基础系统&#xff08;此处的JVM将为您完成此选择&#xff09;。 我仍然记得2013年夏天&#xff0c;当时我正在运行一个项目&#xff0c;整个应用程序中只有1个URL使服务器瘫痪。 问题很简单-机器人决定以很高的速率索引我们的网站&#xff0c;并且该机器人正在创建…

OData V4 系列 查询操作

OData 学习目录 对OData的操作&#xff0c;主要是查询&#xff0c;下面把相关的查询情况列出来&#xff0c;供参考学习&#xff0c;每个操作都有对应的截图&#xff0c;便于理解 默认查询 $expand 查询导航属性关系 &#xff0c;查询Product相关的Supplier $top、$skip、$orde…

JSP项目打开不通的查看详情页动画是放大状态的解决办法

背景:前段时间做了一个详情页在当前页面的放大缩小的动画效果,——>我是如何用Jquery实现网页缩小放大的 今天测试反馈:详情页是缩小状态,点击关闭后打开其他的查看详情页页面,还是默认的缩小状态,需要做成,每次打开默认是放大的效果。 截图: 这个系统比较老,boots…

CentOS7--yum安装

1、创建yum文件夹 [roottester ~]# cd /usr/local/ [roottester local]# ls aegis bin etc games include lib lib64 libexec sbin share src [roottester local]# mkdir ./yum [roottester local]# cd yum2、下载yum源文件 http://mirrors.163.com/centos/7/os/x86…

单点登陆的三种实现方式

背景:单点登录(Single Sign On, SSO)是指在同一帐号平台下的多个应用系统中,用户只需登录一次,即可访问所有相互信任的应用系统。举例来说,百度贴吧和百度地图是百度公司旗下的两个不同的应用系统,如果用户在百度贴吧登录过之后,当他访问百度地图时无需再次登录,那么就…

gorm 密码字段隐藏_KeeWeb for mac(密码管理工具)

KeeWeb是一个非常专业的密码管理工具。这款工具支持Mac OS X&#xff0c;Windows和Linux平台&#xff0c;不需要任何安装和工作在所有现代浏览器&#xff0c;搜索任何条目或查看所有文件中的所有项目作为一个列表。功能非常强大实用。本站现在提供KeeWeb Mac版下载&#xff0c;…

zabbix主动、被动检测的详细过程与区别

最近项目再写采集器采集软件指标的功能&#xff0c;借此机会学习学习zabbix监控的一些知识。 http://www.ttlsa.com/zabbix/zabbix-active-and-passive-checks/ zabbix agent检测分为主动&#xff08;agent active&#xff09;和被动&#xff08;agent&#xff09;两种形式&…

Centos7下按装Docker和docker-compose及环境配置

删除之前安装的 sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-selinux \docker-engine-selinux \docker-engineyum remove docker-ce docker-ce-selinux container-selin…

你也在你的应用上添加B站上的弹幕效果

背景:现在弹幕已成为各大视频网站的标配,其实,弹幕最早是诞生于日本的一个二次元网站Niconico。后来A站和B站将其引入,开启了国内弹幕文化的先河。 开源地址:https://github.com/zerosoul/rc-bullets 相比点赞、转发、评论,弹幕的形式让用户的互动性更强,因此也更受大家…

google earth pro 64位_七彩虹秀精准刀工!iGame RTX 3060 Ti Advanced OC评测:烤机3小时64度...

一、前言&#xff1a;精准控制散热 七彩虹展现神奇刀法这次没有让大家等太久&#xff0c;在RTX 3080上市仅仅2个月之后&#xff0c;RTX 3060 Ti也终于来到了大家面前。这也是安培构架第一款售价在3000元以下的甜点产品。七彩虹公司也同步发售了5款iGame RTX 3060 Ti系列显卡&am…

伪类

伪类 - 元素的特殊状态&#xff08;一般为动态状态&#xff0c;在获得一个状态的同时&#xff0c;将失去另一个状态。&#xff09; 后来扩展为&#xff0c;逻辑上存在&#xff0c;但是在DOM中无需标示的分类。 <a> 标签专属的四个伪类 :link - 未访问状态&#xff08;默认…

流性能

当我阅读Angelika Langer的Java性能教程时-Java 8流有多快&#xff1f; 我简直不敢相信&#xff0c;对于一个特定的操作&#xff0c;它们花费的时间比循环要长15倍。 流媒体性能真的会那么糟糕吗&#xff1f; 我必须找出答案&#xff01; 巧合的是&#xff0c;我最近观看了一个…

javaScript学习笔记之类型转换

背景: JavaScript 数据类型 在 JavaScript 中有 6 种不同的数据类型: stringnumberbooleanobjectfunctionsymbol3 种对象类型: ObjectDateArray2 个不包含任何值的数据类型: nullundefinedtypeof 操作符 你可以使用 typeof 操作符来查看 JavaScript 变量的数据类型。 <…

CentOS安装最新版本curl

CentOS系统自带的curl版本&#xff1a; curl --version curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.53.1 zlib/1.2.7 libidn/1.28 libssh2/1.8.0版本比较低。 想要从源代码构建最新的cURL&#xff1f; 先决条件 安装所需的软件包 yum install wget gcc …

Redis-cluster集群【第一篇】:redis安装及redis数据类型

Redis介绍&#xff1a; 一、介绍redis 是一个开源的、使用C语言编写的、支持网络交互的、可以基于内存也可以持久化的Key-Value数据库。redis的源码非常简单&#xff0c;只要有时间看看谭浩强的C语言&#xff0c;在去看redis的源码能看懂50-60%。redis目前最大的集群应该是新浪…

ad10怎么挖铺的铜_黄金怎么验真假,简单易行方法多。

在当今社会&#xff0c;随着人们生活水平的提高&#xff0c;大家越来越追求生活的质量。也就形成了我们在生活中想通过各种细节丰富我们的生活&#xff0c;提升我们的生活情趣。导致我们对物质的追求也上了一个台阶&#xff0c;相比之下黄金市场的需求也大大增加。而我们大家的…

CMD启动mysql服务“发生系统错误 5”的解决办法

背景&#xff1a;cmd进入&#xff0c;启动mysql报&#xff1a;发生系统错误 5 问题&#xff1a; 解决办法&#xff1a; 找到cmd.exe的位置&#xff0c;不好找请使用以下命令。 找到文件&#xff0c;右键以管理员身份运行即可。

jQuery.extend 函数详解

JQuery的extend扩展方法&#xff1a;Jquery的扩展方法extend是我们在写插件的过程中常用的方法&#xff0c;该方法有一些重载原型&#xff0c;在此&#xff0c;我们一起去了解了解。一、Jquery的扩展方法原型是:   extend(dest,src1,src2,src3...);它的含义是将src1,src2,sr…