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 ))