- 浏览: 95461 次
- 性别:
- 来自: 上海
最新评论
-
shmily2038:
有相应的例子? 给的全部是英文,还不如自己看官网e文。 上代 ...
ActiveMQ集群随记 -
lutian1984:
你好,你验证过你转发的这个东西吗?为什么在我这里还是报错呢?
jquery.form.js ajax上传文件问题 -
chenhongwei0924:
精辟.
如何防止SQL注入 -
fairyhawk:
简单的几句,经验的总结。
如何防止SQL注入 -
joliny:
谢谢了,这个问题也困扰了我很久、!
Apache整合Tomcat后get方式提交中文乱码问题解决
I’ve recently switched from using JUnit 3.x to JUnit 4 for most new unit tests I write. One area that causes trouble is the use of JUnit 3.x based test base classes. Spring
has a tree of helper classes based on junit.framework.TestCase
that make writing tests that use an ApplicationContext, dependency injection or transactions easier. These base classes can autowire your test so that your member variables are initialized and ready to go, pointing to beans managed by Spring.
I wanted the functionality provided by these base classes but wanted to continue to write tests in the JUnit4 style. The two best features of JUnit 4 are the @BeforeClass/@AfterClass
annotations and the ability to have multiple setUp and tearDown methods using @Before
and @After
. Switching back to JUnit 3 just to use the base class seemed unfortunate.
My first attempt was to extend my JUnit 4 test from Spring’s class, AbstractDependencyInjectionSpringContextTests
. When JUnit runs, it inspects your class through reflection and decides if it’s a JUnit 3 or 4 class. It sees that since Spring’s class derives from junit.framework.TestCase and so runs it as a JUnit 3 test using org.junit.internal.runners.OldTestClassRunner
.
You can circumvent JUnit’s decision about this test and force it to be run as a JUnit 4 test instead using the annotation RunWith((TestClassRunner.class)
. This forces JUnit to use the new test runner (TestClassRunner) even though the class derives from TestCase
. I created a class called SpringManagedTests
that derives from AbstractDependencyInjectionSpringContextTests
and is annotated with RunWith
. (My tests will extend SpringManagedTests
).
Next, JUnit 4 only cares about annotations and won’t run the old setUp and tearDown methods defined by the Spring classes. To get around this, add methods with Before
and After
annotations to call setUp and tearDown.
@Before final public void callSetup () throws Exception { super . setUp (); } @After public void callTearDown () throws Exception { super . tearDown (); }
All features of the base class are now available, and JUnit 4 annotated test methods run as you would expect. The final test base class is below. To use it, derive your test from SpringManagedTests
and read the docs on AbstractDependencyInjectionSpringContextTests
for information on having Spring wire members of your class. In my case I use field injection – I have member variables declared as protected with the field name matching the bean name. When the test runs the field are populated.
This technique could be used for other JUnit 3 based test frameworks as well. It’s not as clean as having a pure JUnit 4 implementation but works well until that happens.
import org. junit. After; import org. junit. Before; import org. junit. internal. runners. TestClassRunner; import org. junit. runner. RunWith; import org. springframework. test. AbstractDependencyInjectionSpringContextTests; /** * A base class for tests that load a spring application context * and use dependency injection to populate fields in the test class. * Unlike AbstractDependencyInjectionSpringContextTests, this * class can be used as a base class for JUnit 4 tests. * @author dyoung */ // RunWith is required to force what would otherwise look like a JUnit 3.x test // to run with the JUnit 4 test runner. @RunWith ( TestClassRunner. class ) public class SpringManagedTests extends AbstractDependencyInjectionSpringContextTests { // pass through to the junit 3 calls, which are not annotated @Before final public void callSetup () throws Exception { super . setUp (); } @After public void callTearDown () throws Exception { super . tearDown (); } } From : http://dmy999.com/article/21/adapting-a-springs-junit-3x-base-classes-to-junit-4
发表评论
-
佳文收藏 - How to redirect a web page, the smart way
2010-08-05 10:48 1630本文引自:http://www.stevenhargrove. ... -
关于sojo输出json中出现~unique-id~字样的问题
2010-07-20 10:27 3087问题:在项目中,问题的表现如下,使用SojoJsonStrin ... -
jquery.form.js ajax上传文件问题
2010-07-20 10:25 4832问题:使用jquery.form.js实现ajax上传文件功能 ... -
Ehcache集群随记
2009-11-10 09:28 1711Distributed Caching with ehcach ... -
ActiveMQ集群随记
2009-11-10 09:28 3922Problem: cluster on JMS queue o ... -
常用第3方类库
2009-11-02 10:47 795转自:http://www.iteye.com/n ... -
项目中一次正则表达式的实践
2009-10-23 17:13 942今天在项目中遇到这样一个需求: 要求把一段HTML代码中的注释 ... -
DateFormat
2009-08-20 17:01 800from http://shib.kuleuven.be/do ... -
A few frequently used SSL commands
2009-08-19 13:13 1119常用导入证书 %JAVA_HOME%\jre\lib\secu ... -
缓存比较笔录
2009-07-29 18:08 996ehcache 比较常用的轻量级缓存框架,是hibernate ... -
CAS单点登录入门使用
2009-07-29 16:34 984单点登录------CAS http://zhyerr.blo ... -
MySQL数据库引擎介绍
2009-07-29 16:28 1707如果你是个赛车手,并 ... -
在Sql Server中使用pst根据字符型类型查询的性能问题
2009-03-23 10:59 958问题:在使用mssqlserver的jdbc时,当根据字符型列 ... -
Ibatis事务的一些小结
2009-03-11 10:33 3114问题发生:原先在使用Ibatis的时候进行insert, up ... -
Ajax应用的安全性小结
2009-02-12 09:47 829对Ajax应用的安全性进行一下小结: 1.基于各浏览器的ser ... -
如何防止SQL注入
2009-02-10 10:05 4090归纳一下,主要有以下几点: 1.永远不要信任用户的输入。对用户 ... -
How to make thread safe
2009-01-11 16:39 951How to make thread safe 1.Use i ... -
Java怎样中断一个运行中的线程
2009-01-09 10:15 1009程序是很简易的。然而,在编程人员面前,多线程呈现出了一组新的难 ... -
Apache整合Tomcat后get方式提交中文乱码问题解决
2009-01-08 11:34 2642我在Tomcat中的8080的connector里配置了URI ... -
Quartz 与 Spring 配置注意事项
2008-08-29 18:05 1449在Spring配置和Quartz集成内容时,有两点需要注意 1 ...
相关推荐
New.Riders.Adapting.to.Web.Standards.CSS.and.Ajax.for.Big.Sites
红杉:Adapting to Endure_2022_05
信息安全_数据安全_More_than_Vaulting:Adapting_to_N 硬件攻防 解决方案 安全研究 数据分析 安全对抗
What’s Inside Preface 1 Java SE5 and SE6 .................. 2 Java SE6 ......................................... 2 The 4th edition........................ 2 Changes .....................................
The functionality to track site usage is still provided as of IBM® WebSphere® Portal version 6.0.x, but the Tivoli® Web Site Analyzer that charts the resulting log files from version 5.1 was ...
Adapting_to_Endure_May_2022
Adapting to Change......Page 54 Responding to Threats......Page 55 Firewalling Tens of Thousands of Workloads with a Single Logical Firewall......Page 57 Achieving Micro‐segmentation......Page 59 ...
Adapting Ranking SVM to Document Retrieval RankSVM
Take a tour of the new appearances and behaviors that Cocoa supports and learn how easy it is to adopt them when you use standard Cocoa classes. Get an overview of the changes to Cocoa and find out ...
Why should you use Scrum in your web projects? Simply put, it'll enable your team to get more done in less time. Scrum is the most popular agile project management methodology used ... Adapting to Scrum
Why should you use Scrum in your web projects? Simply put, it'll enable your team to get more done in less time. Scrum is the most popular agile project management methodology used ... Adapting to Scrum
This documents describes the API of JVoiceXML from the user’s point of view. It provides information about the coding of clients for the JVoiceXML voice browser. 目录 1 Introduction 3 2 ...
Adapting a Pedestrian Detector by Boosting LDA Exemplar Classifiers
Section D.3. SNMPv3 Protocol Section D.4. SNMP Agent Extensibility Section D.5. SMIv1 MIB Modules Section D.6. SMIv2 MIB Modules Section D.7. IANA-Maintained MIB Modules Section D.8. ...
• The code base has been entirely redesigned to take advantage of the features of Python, such as use of generators for iterating elements of a collection. • Many algorithms that were presented as ...
Manager is progressively and intelligently adapting it’s CPU usage to the project’s global performance. When you will use tons of dynamic optimizer objects it’s update rate will imperceptibly slow...
Adapting a File-like Object to a True File Object Recipe 2.16. Walking Directory Trees Recipe 2.17. Swapping One File Extension for Another Throughout a Directory Tree Recipe 2.18. Finding a ...
This book shows you how to start adapting the language now to realize its benefits on today’s browsers. It concentrates on the practical—the problems HTML5 can solve for you right away. By following...
Key Features ...This book is packed with real-... Adapting to MVC Chapter 7. Testing Web Applications Chapter 8. Using Existing PHP Frameworks Chapter 9. Building REST APIs Chapter 10. Behavioral Testing
f f Adapting the default behavior of some concurrency classes to your needs f f Testing Java concurrency applications f f What this book covers Chapter 1, Thread Management will teach the readers how ...