Spring 对于单元测试来说,我感觉还是挺方便的。
我们可以用maven来构建项目,这样对于包的依赖就交给maven处理。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.myapp</groupId> <artifactId>guiapp</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>logGUIApp</artifactId> <packaging>jar</packaging> <name>my app </name> <description>log in interface</description> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-atinject_1.0_spec</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.1.RELEASE</version> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> </project>
大概就这么一个POM 文件
接下来我们还是关注如何使用Spring做单元测试。
还是来一个AbstractSpringTestCase 母类。
import java.io.File; import javax.annotation.Resource; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This class provides convenient afterTestClass and beforeTestClass methods from Spring Framework which subclasses can * override to perform actions once per test class. * */ @RunWith(SpringJUnit4ClassRunner.class) public abstract class AbstractSpringTestCase extends AbstractJUnit4SpringContextTests { protected static File basedir = new File( System.getProperty( "basedir", "." ) ); protected static File builddir = new File( basedir, "target" ); @Resource protected ApplicationContext applicationContext; /** * This allows eclipse and maven to have the same basedir */ @BeforeClass public static void beforeBaseClass() { System.setProperty( "basedir", System.getProperty( "basedir", "." ) ); System.setProperty( "derby.system.home", System.getProperty( "derby.system.home", "./target" ) ); } }
大家看到这个类的一个注解 @RunWith 也就是JUnit使用了指定的类来启动测试
然后就交给SpringJUnit4ClassRunner 了。这个类 会寻找你的配置文件,这里我更喜欢用注解来指定
请看一个简单测试类。
@ContextConfiguration(locations = { "classpath:/com/myapp/guiapp/jdbc/test/applicationContext.xml" }) public class JdbcTest extends AbstractSpringTestCase { @Inject private MyAppJdbcConnectionFactory myAppJdbcConnectionFactory; @Inject private Storage storage; @Inject private TcpServerTask tcpServerTask; private int port; @Before public void before() { port = tcpServerTask.getPort(); } @BeforeClass public static void beforeClass() throws Exception { System.setProperty( "javax.net.ssl.keyStore", basedir + "/target/etc/mc.ks" ); System.setProperty( "javax.net.ssl.trustStore", basedir + "/target/etc/mc.ts" ); System.setProperty( "javax.net.ssl.keyStorePassword", "changeit" ); System.setProperty( "javax.net.ssl.trustStorePassword", "changeit" ); } @Test public void testJdbcDirectAccessToStorage() throws Exception { Connection connection = null; try { // just enough to get JDBC connect direct to storage with same jvm, ie no tcpserver involved String goodUrl = "jdbc:myapp:DummyTable"; String badUrl = "jdbc:myapp:DummyTableBad"; try { connection = myAppJdbcConnectionFactory.getConnection( badUrl, "", "", null ); Assert.fail( "Able to create connection with invalid URL" ); } catch ( DataProviderException e ) { } connection = myAppJdbcConnectionFactory.getConnection( goodUrl, "", "", null ); Assert.assertNotNull( connection ); } finally { ConnectionUtils.closeQuietly( connection ); } } }
@ContextConfiguration 指定了此测试类使用的bean配置文件。
在这个文件中,我们就可以定义测试需要的一些bean
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:com/myapp/jdbc/applicationContext.xml" /> <bean id="tcpServerTask" class="com.myapp.transport.server.internal.TcpServerTask"> <property name="enableSSL" value="false" /> </bean> </beans>
然后就使劲折腾吧。
相关推荐
spring+springMVC+mybatis+maven+junit+mysql
编程语言+JAVAspring+测试...它介绍了JAVAspring的测试框架的概念、原理和作用,以及如何使用JAVAspring的测试框架来进行单元测试,包括JUnit、TestNG、Mockito、Spring Test等内容,以及一些配置文件和注解的用法。
使用java框架: hibernate + struts + spring + bootstarp + mahout 实现功能: 后台图书管理: 图书的增删改查, 后台类目。 前台书店: 图书的分页查看, 前台类目, 多条件查询, 智能推荐, 购物车, 支付...
Spring5 框架的保姆式教程+笔记,...(本教程附带Spring所有需要的jar包+Junit的4.1.2jar包) (该教程,需要有java se基础和一定web开发基础,笔记使用Typora文档,如果图片无法显示请自行修改笔记中图片的所在路径)
12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...
主要介绍了Spring+Junit4进行接口测试实例代码,涉及pom.xml、bean的配置,以及接口测试代码等相关内容,小编觉得还是挺不错的,这里分享给大家,需要的朋友可以参考下
这是Spring开发最基础的四个包(beans,core,context,expression),另外还附加了日志包和Junit包,用于最基础的Spring开发,适用了刚学Spring或是找不到包的学生,希望能帮到大家,也祝愿大家能更快的入手spring,...
主要介绍了idea +junit单元测试获取不到bean注入的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
该功能是业余时间写的东东,为了方便使用连接数据库,项目进行测试,方便使用spring配置定时任务,excel 读写操作等(项目是完整配置的东东),相关包由于文件太大无法上传,放入共享文件夹,有需要可以查看 ...
开发中经常用到,可以直接下载来用,另外添加了jstl、junit、mysql、dbcp连接池
java-5.1.21.jar servlet-api-3.0-alpha-1.jar spring-aop-3.2.0.RELEASE.jar spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE....
Spring Boot 2 + JUnit 5示例应用程序1.将JUnit 5依赖项添加到pom.xml < dependency>< groupId>org.junit.jupiter</ groupId>< artifactId>junit-jupiter-api</ artifactId>< scope>test</ scope></ dependency>...
主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
由于很多人不会配置Junit4和spring boot集成,所以文档主要讲解spring boot Junit4配置
技术全面:涵盖Java Web开发的主流技术和主流框架; 工具详尽:包括JDK、Tomcat、Eclipse、MySQL、Log4j、Ant、JUnit、CVS、解析XML文件等; 由浅入深:每章从简单示例入手,最后给出一个详细示例加深理解; 实战性...
4、Spring web环境及其Junit的集成 5、Spring JDBCTemplate的基本使用 6、Spring AOP的介绍及其配置使用 7、Spring的声明式事务控制 8、SpringMVC的介绍及其快速入门 9、SpringMVC的数据请求和响应 10、SpringMVC的...
spring框架单元测试包。一般程序员在导入测试jia包时需要下载导入jar包,java测试包junit-4.9的jar包已压缩好。
system-rules-1.16.1-javadoc.jar
用Cucumber-Java-Maven-Junit-Jenkins进行Api测试 在詹金斯中运行测试案例 詹金斯的Cucumber报告
================================================== ================================================== ========================== 一般信息GW-SERVER 项目用于持久化“测验”数据:问题、用户记录等。...