How to Use Mockito for unit testing in Spring

Source: Internet
Author: User

However, in the service layer and persistence layer, if we use annotation injection to design it, It is very troublesome to replace the actual injection class with Mock. Fortunately, we have a Mockito framework. However, for a piece of code, you will find that it is much easier to use than easyMock [java] @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = {"classpath *: application-context-test.xml"}) public class RouteServiceTestCase {@ InjectMocks @ Autowired private IRouteService service; @ Mock private IRouteMatrixDataProvider provider; @ Before public void myBefore () {MockitoAnnotations. initMocks (this) ;}@ Test public void testGetAirlineCode () {RouteMatrix rm = new RouteMatrix (); rm. setAirlineCode ("kkk"); Mockito. when (this. provider. getRevenueRoute ("HKG", "AMM", true )). thenReturn (rm); String code = this. service. getAirlineCode ("HKG", "AMM", this. brand, this. cInfo, true); Assert. assertNotNull (code); Assert. assertEquals ("kkk", code); code = this. service. getAirlineCode ("HKG", "KKK", this. brand, this. cInfo, true); Assert. assertNull (code );} @ Test public void testGetAirlineCode2 () {String code = this. service. getAirlineCode ("HKG", "AMM", this. brand, this. cInfo, true); Assert. assertNotNull (code); Assert. assertEquals ("kkk", code) ;}@ Spy @ Autowiredprivate IRouteMatrixDataProvider provider; the service is marked with @ InjectMocks and executed MockitoAnnotations in the myBefore method. when initMocks (this); is used, the attributes marked with @ Mock or @ Spy are injected into the service. If it is @ Mock, it is the normal method. The provider in the service is completely replaced by the Mock instance, and all calls are for the Mock generation class. If it is @ Autowired plus @ Spy, the Mock instance will be called if the returned value is customized. Otherwise, the property of the actual injection will be called, but there is a limitation here. If it is a proxy class, an error will be reported, for example, Spring's AOP proxy. Www.2cto.com for the AOP proxy class, if you want to use Mock in part, and use real instances in part, it feels a little difficult at present. The current method is to write two test classes and inject real attributes into one, A mock injection attribute. If you do not want to specify a specific parameter during method calling, you can use the following method to represent any parameter. [Java] Mockito. when (this. provider. getRevenueRoute (Matchers. anyString (), Matchers. anyString (), Matchers. anyBoolean ())). thenReturn (rm); but here there is also a limit that if any *** () is used, all parameters must use this method, you cannot write [java] Mockito as follows. when (this. provider. getRevenueRoute (Matchers. anyString (), Matchers. anyString (), true ))

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.