On the application of reflection and Jmock in JUnit test

Source: Internet
Author: User

Recently, due to the needs of the project, the developed code enforces JUnit testing and achieves a certain coverage probility (mainly for customers). So, in order to achieve a certain coverage, I have to use a lot of reflection in case (usually for private methods) and Jmock (mainly for some property methods).

Small series is to do development background, because of the spring source code has a more or less understanding, so the reflection technology also have a certain understanding. Therefore, in the process of using reflection for junit testing, there are not too many problems. The small part thinks that the most important thing to use reflection is to distinguish between two points: one is to reflect the implementation of the Class (remember, the object type here must be a concrete implementation class, not an interface), and the second is to invoke the method through reflection (generally referred to as the Private method), here need to pay attention to the method of refactoring; The most headache is left Jmock way.

In fact, I am ashamed, Small series contact Jmock or in this November, was sent to a pure backstage project; At the time, I thought Jmock was just a technology for junit testing, and there's nothing too much for our developers to know about. I feel like my buddies don't have to jmock. The same can be done to JUnit, the same can improve coverage; As unit testing progresses, I slowly discover that some methods, if not used Jmock, spend much more time and effort than using Jmock, due to the project rush time, The company does not have much time for us to spend on junit test, so I feel I should reiterate my position, I feel that I should know about Jmock, not only to speed up the current JUnit test progress, but also to add their knowledge on junit testing;

(Note: The following is the author's own understanding, if there is not appropriate, but also hope that you give suggestions, small in this first thank you!!!!) )

1. The first is to create Mockcontext, which is used to construct the method object to be mock.

2. Construct a concrete mock object from the Mockcontext created by Step1.

Eg:mockContext.mock (Class,objectagentname);

The argument in the mock method, class refers to the type of mock object to be constructed, which can only be an interface; Objectagentname refers to the name of the proxy of the mock object, which is not a required parameter; The default proxy name is currently created based on class;

Note: In Mockcontext, the proxy name is unique, that is, two mock objects with the same name cannot appear.

3. Use the Mockcontext.checking () method to replace the mock object with the object in the specific code.

eg

Mockcontext.checking (new expectations () {

{

    1. Specific parameters, when the parameter is "Dandan", the Addressservcie object's Findaddresses method returns a Iterator<address> object with Returniterator.

Allowing (Addressservcie). Findaddresses ("Dandan");

Would (Returniterator (addresses));

    

2. Parameter type, when the argument is of type string, the Findaddresses method of the Addressservcie object returns a Iterator<address> object with Returniterator.

Allowing (Addressservcie). Findaddresses (Nonull (any(string.class)));

Would (Returniterator (addresses));

    

When the argument is "Dandan", the Findaddresses method of the Addressservcie object returns a Iterator<address> object with Returniterator.

Oneof (Addressservcie). Findaddresses ("Dandan");

Would (Returniterator (addresses));

  

When the mock method object is executed multiple times in the code.

AtList (1). of (Addressservcie). Findaddresses (Nonull (Any (string.class)));

Would (Returniterator (addresses));

}

});

On the application of reflection and Jmock in JUnit test

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.