Simplifying mocks in tests with Spring2.5 and reflection

Source: Internet
Author: User
Tags new features reflection reset stub

SPRING2.5 's biggest feature is the full use of annotation in place of XML configuration, including the IOC Container, SPRINGMVC and TestContext test framework, which has brought great convenience to our development. The new features of SPRINGMVC have been described in more detail in this article, and for spring's new TestContext test framework, you can also get detailed examples from here, interested to read carefully, this article will not repeat. All in all, through the annotation provided by spring2.5, we can make our classes--including controller,test--special types of responsibilities--more POJO, easier to test, and more efficient testcase development.

In the development process, we often need to mock specific objects to test the expected behavior, or use stub objects to improve unit test efficiency. The most common example is the method of mocking or stub the underlying DAO class in the Controller class's test method in a multi-tier webapp, thus reducing the cost of database operations while unit testing, and speeding up unit test rates. As for reflection, it is no longer a new concept of Java, and all kinds of frameworks basically use reflection to enhance the dynamics of runtime. In Java5, the reflection efficiency and the introduction of annotation greatly improve the dynamic of the Java language, and let the developers get more runtime flexibility. This article will demonstrate how to use spring2.5 and reflection to simplify mocks in tests, and the JUnit framework used is the Junit4.4,mock framework is Easymock2.4.

Let's take a look at the most primitive tests that use mock objects (assuming development based on JDK5, with new features including Static Import,varargs):

Import static org.easymock.easymock.*;
public void HelloWorldTest extends Abstractsinglespringcontexttests {
private foo foo = Createmock (Foo.class);
Private Bar bar = Createmock (Bar.class);
Private Helloworld Helloworld;
@Before
public void before () {
Reset (foo, bar);
HelloWorld = new HelloWorld (foo, bar);
}
@After
public void after () {
Verify (foo, bar);
}
@Test
public void Shouldsayhello () {
Set expectations about Foo/bar
Replay (foo, bar);
Helloworld.sayhello ();
Assert verification
}
//
}

As you can see, with the old Spring version of TestContext, there are at least two aspects of the above code that need to be enhanced:

1. A large number of mock object creation operations are required, cumbersome code unrelated to the true test case, and an inheritance dependency on the spring context test class is introduced

2. For different Test classes, each time you use a different mock object, you need to explicitly specify the mock object used by the Reset/replay/verify

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.