在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

Spring 3使用JUnit 4进行测试– ContextConfiguration和AbstractTransactionalJUnit4SpringContextTests...相关推荐

  1. Spring MVC控制器JUnit测试

    JUnit测试Spring MVC控制器并非易事 . 但是最近,一个新项目 (即将在Spring推出)提供了新工具来简化此工作. 这篇文章说明了如何通过JUnit测试来测试一个简单的控制器. 该代码是 ...

  2. Spring集成文件轮询和测试

    我最近实施了一个小项目,在该项目中,我们必须轮询文件夹中的新文件,然后在文件内容上触发服务流. Spring Integration非常适合此要求,因为它带有一个通道适配器 ,该适配器可以扫描文件夹中 ...

  3. Spring 单元测试(Junit)

    单元测试(Unit Test),是一段自动化的代码,用来调动被测试的方法或类,而后验证基于该方法或类的逻辑行为的一些假设.单元测试几乎总是用单元测试框架来写的.它写起来很顺手,运行起来不费时.它是全自 ...

  4. Spring JdbcTemplate RowMapper Junit Test Example

    Spring JdbcTemplate RowMapper Junit Test Example 文章目录 Spring JdbcTemplate RowMapper Junit Test Examp ...

  5. 单元测试之JUnit 5 参数化测试使用手册

    1. 概要 junit5是下一代JUnit测试框架,新增了很多特性帮助开发人员更好得编写测试用例.其中一大特性就是参数化测试,其目的就是让我们可以使用不同的参数多次执行一个测试方法,从而覆盖不同的条件 ...

  6. junit动态忽略测试_有条件忽略测试的JUnit规则

    junit动态忽略测试 我一直认为使用@Ignore停用测试是一个坏主意. 例外,这可能是一种将间歇性失败的测试放入隔离区以供以后处理的方法(如Martin Fowler 在此处所述 ). 随着越来越 ...

  7. Spring Boot微服务的黑匣子测试是如此简单

    当我需要进行原型设计,概念验证或在空闲时间使用一些新技术时,开始新项目对于Maven来说总是有点烦人. 不得不说,设置Maven项目并不难,您可以使用Maven原型. 但是原型通常是过时的. 谁想玩旧 ...

  8. IDEA - 解决“idea 环境:junit:4.11 测试 mybatis 代码,无法导入 org.junit.Test 包”问题

    IDEA - 解决"idea 环境:junit:4.11 测试 mybatis 代码,无法导入 org.junit.Test 包"问题 参考文章: (1)IDEA - 解决&quo ...

  9. springboot集成Junit在执行测试类的时候提示Failed to resolve异常

    项目场景:springboot集成Junit在执行测试类的时候提示Failed to resolve异常 项目场景:springboot版本2.5.7,Junit版本:4.13.2,在执行测试类的时候 ...

最新文章

  1. CentOS下的DNS服务器搭建
  2. TensorFlow:将ckpt文件固化成pb文件
  3. 编程没有捷径 奇葩冒牌程序员的故事
  4. 怎么改变光标Cursor(5种方法)
  5. JQuery EasyUI combobox(下拉列表框)
  6. linux mysql提示1045_linux mysql ERROR 1045
  7. 从门外汉到 Go 圈网红技术博主的五年历程
  8. 告别ASP.NET操作EXCEL的烦恼(总结篇)
  9. HelloDjango 第 06 篇:博客从“裸奔”到“有皮肤”
  10. 4.6 GoogLeNet CNN、tensorflow实现——python实战
  11. 抢票软件开发(二) 模拟登录
  12. mgo 的 session 与连接池
  13. docker 创建redis容器
  14. 难以置信!网易首席架构师竟用了500页笔记,把网络协议给趣谈了
  15. xp桌面上的计算机管理,如何解决WinXP系统开机桌面变白显示恢复Active Desktop的问题?...
  16. 【Jenkins+青藤云】基于Jenkins部署青藤云镜像扫描插件(1)
  17. Cannot locate the chosen ObjectFactory implementation: spring - [unknown locati
  18. 夏培肃完成了第一台电子计算机运算器,,学生支部述职报告2017
  19. 热议话题回顾:数据上云和迁移中可能会遇到哪些问题-曾文旌
  20. 平面设计资源网站,码住!

热门文章

  1. 集成Springboot----ElasticSearch
  2. stream进行分组统计
  3. chengxuyuan
  4. jvm(8)-虚拟机字节码执行引擎
  5. removeAll throws java.lang.UnsupportedOperationException
  6. HDU1576(欧几里得算法)
  7. JDK8的日期时间类3
  8. camel apache_Apache Camel 3的工作终于开始了
  9. 本地构建和自动化构建_构建自动化面板
  10. hazelcast入门教程_Hazelcast入门指南第4部分