????1.?????
????????????spring??mockito
????2.????
???????????????????????б????????????????????????????Щ???????????????????????mock???????в???????????Ч?????????????spring??mock?????У?mock?????????????????????????????????????????
????3.??????
????Mock?????????????????????????????spring??????junit4????TestExcutionListeners??????????????????н?mock?????????????????С?
????4.????????????
private IAccessServiceaccessService = Mockito.mock(IAccessService.class);
@BeforeClass
public static void beforeClass(){
// ???????Mock????ICmsProxyService
sceneConfigService.setAccessService(accessService);
}
????5.?????????????
????5.1  ????????
???????DependencyInjectionTestExecutionListener??
???????injectDependencies(TestContexttestContext)????
public class MockitoDependencyInjectionTestExecutionListener extends
DependencyInjectionTestExecutionListener {
@Override
protected void injectDependencies(TestContext testContext) throws Exception {
super.injectDependencies(testContext);
init(testContext);
}
????5.2 ???÷??????mock????
private void init(final TestContext testContext)throws Exception {
Object bean = testContext.getTestInstance();
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if(annotationinstanceof Mock){
//???Mock???
MockObject obj = new MockObject();
obj.setType(field.getType());
obj.setObj(Mockito.mock(field.getType()));
field.setAccessible(true);
field.set(bean?? obj.getObj());
mockObjectMap.put(field.getName()?? obj);
}else if(annotation instanceofAutowired){
injectFields.add(field);
}
}
}