To mock a class member variable using Mockito

Source: Internet
Author: User


The Chinese meaning of a mock is impersonation, Mockito is a mock object framework, primarily for unit testing, which returns our desired results by hiding real implementations, so that we can exclude other factors that might affect the execution of the current unit. If one of our features calls for a remote interface, but this time we only need to be concerned about whether the current functionality works properly without having to worry about whether the remote interface is working properly, we can test it by simulating the correct or incorrect values returned by the remote interface.
Here are two articles about Mockito that can be quickly started:

http://blog.csdn.net/onlyqi/article/details/6396646
http://blog.csdn.net/onlyqi/article/details/6546589

This introduces the basic use of Mockito, as well as the available examples, for the first contact of the Mockito people, is a good entry document.
The following is the example of the Mockito itself in Javadoc, but it is all in English, and the two articles mentioned above have similarities, but will be introduced more than a few interesting words to see:

Http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html

In the actual operation process, we often mock the variables of the class members, especially the class member variables of classes in the third class library mock, at this time we can not modify the source code of Third-party Libraries, we have two ways to achieve this at this time:
1, by using the way to continue
This approach, however, is limited by the variable definition scope of the mock class member variable, only public and protected can use this method, as follows a class of a third-party library:

public class oneclass{
	protected Testa testa;
	//......
}


At this point, we can use a class in the mock test class to inherit the class, and then pass the variable to the parent class through the super invocation of the subclass:

Private class Oneclasschild extends oneclass{public
		oneclasschild (Testa testa) {
			super.testa = testa;
		}
	}


Our mock code can be written like this:

@Test public
void Testmock () {
	Testa testa = new Testa ();
	Oneclass Oneclass = new Oneclasschild (testa);
	Suppose you need to invoke the method Calloneclassmethod,return returns the When
	(Oneclass.calloneclassmethod ()) According to the actual situation. Thenreturn (0);
	//......
 
}

This achieves a mock of the purpose of the class member variable Testa in Oneclass, and replaces the actual return result with the return result we need.

2, by using the way of reflection
The member variables of a class in a third party class library are private:

public class oneclass{
	private Testa Testa;
	//......
}


This is the time to replace the member variables in this class by reflection, and the mock code will be as follows:

@Test public
void Testmock () {
	Testa testa = new Testa ();
	Oneclass Oneclass = new Oneclass ();
  Field Testafield = Oneclass.getclass (). Getdeclaredfield ("Testa");
	Testafield.setaccessible (true);
	Testafield.set (Oneclass, testa);
	Suppose you need to invoke the method Calloneclassmethod,return returns the When
	(Oneclass.calloneclassmethod ()) According to the actual situation. Thenreturn (0);
	//......
 
}

Compare the above two ways, or the second is more general, not affected by the visibility of variables.

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.