????spring??mockito???????Mock???????
???????????? ???????[ 2014/3/6 14:47:36 ] ??????????????? Mock spring
????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);
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11