001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * dmetzler 016 */ 017package org.nuxeo.runtime.mockito; 018 019import java.lang.annotation.Annotation; 020import java.lang.reflect.Field; 021 022import org.mockito.Mock; 023import org.mockito.Mockito; 024import org.mockito.internal.configuration.FieldAnnotationProcessor; 025import org.nuxeo.runtime.api.DefaultServiceProvider; 026 027/** 028 * @since 5.7.8 029 */ 030public class NuxeoServiceMockAnnotationProcessor implements FieldAnnotationProcessor<Mock> { 031 032 @Override 033 public Object process(Mock annotation, final Field field) { 034 035 Object mock = Mockito.mock(field.getType(), field.getName()); 036 037 for (Annotation ann : field.getAnnotations()) { 038 if (ann.annotationType().equals(RuntimeService.class)) { 039 bindMockAsNuxeoService(field, mock); 040 } 041 } 042 043 return mock; 044 } 045 046 protected void bindMockAsNuxeoService(final Field field, Object mock) { 047 MockProvider provider = (MockProvider) DefaultServiceProvider.getProvider(); 048 provider.bind(field.getType(), mock); 049 } 050 051}