publicclassInjectMocksTest{@MockprivateEJBBeanInjectedByContainerInRealCodedependencyBean;//Create a mock object with type EJBBeanInjectedByContainerInRealCode and inject it to a spy object.//Mockito will try to inject this dependency to target object in this order: Constructor, Property, Field.@InjectMocks@SpyprivateMyEJBBeanejbBean;//Create a spy object of MyEJBBean, all fields annotated with @Mock with injected into this spy object.@BeforepublicvoidsetUp(){initMocks(this);//This is required to create all the mock objects and spy objects declared in annotation way.}@TestpublicvoidtestShouldXXX()throwsException{when(dependencyBean.findSomeDataFromDB(anyString(),anyString(),anyString())).thenReturn(anyList());//Mock the behavior the the injected dependency.Object[]objects=ejbBean.myMethod();//Call the real method on the spy object.assertThat(objects).isNotEmpty();}}
Verify method call
Verify method called with expected arguments
123456789
ArgumentCaptor<String>argument=ArgumentCaptor.forClass(String.class);verify(errors,atLeastOnce()).add(argument.capture(),any(ActionMessage.class));//Verify errors.add(_, _) called with expected arguements, mockito will store captured arguemnt into the ArgumentCaptor object.List<String>values=argument.getAllValues();//If method called only once, use getValue() to get the value passed in, if caleed multi times, use getAllVales() to get value list.assertTrue(values.contains("exception.message"));assertTrue(values.contains("exception.detail"));