How to use method of org.mockito.internal.configuration.InjectingAnnotationEngine class

Best Mockito code snippet using org.mockito.internal.configuration.InjectingAnnotationEngine.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright 2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.powermock.api.mockito.internal.configuration;
17
18import org.mockito.Mock;
19import org.mockito.internal.configuration.InjectingAnnotationEngine;
20
21/**
22 * The same as {@link InjectingAnnotationEngine} with the exception that it
23 * doesn't create/injects mocks annotated with the standard annotations such as
24 * {@link Mock}.
25 */
26public class PowerMockitoInjectingAnnotationEngine extends InjectingAnnotationEngine {
27
28	@SuppressWarnings("deprecation")
29	@Override
30	public void process(Class<?> context, Object testClass) {
31		// this will create @Spies:
32		new PowerMockitoSpyAnnotationEngine().process(context, testClass);
33
34		// this injects mocks
35        injectMocks(testClass);
36	}
37}
38
Full Screen
copy
1/*
2 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Contributors:
17 *     dmetzler
18 */
19package org.nuxeo.runtime.mockito;
20
21import org.mockito.configuration.AnnotationEngine;
22import org.mockito.internal.configuration.InjectingAnnotationEngine;
23import org.mockito.internal.configuration.SpyAnnotationEngine;
24
25/**
26 * @since 5.7.8
27 */
28public class NuxeoInjectingAnnotationEngine extends InjectingAnnotationEngine {
29    private AnnotationEngine delegate = new NuxeoDefaultAnnotationEngine();
30
31    private AnnotationEngine spyAnnotationEngine = new SpyAnnotationEngine();
32
33    /**
34     * Process the fields of the test instance and create Mocks, Spies, Captors and inject them on fields annotated
35     * &#64;InjectMocks.
36     * <p>
37     * This code process the test class and the super classes.
38     * <ol>
39     * <li>First create Mocks, Spies, Captors.</li>
40     * <li>Then try to inject them.</li>
41     * </ol>
42     *
43     * @param clazz Not used
44     * @param testInstance The instance of the test, should not be null.
45     * @see org.mockito.configuration.AnnotationEngine#process(Class, Object)
46     */
47    @Override
48    public void process(Class<?> clazz, Object testInstance) {
49        processIndependentAnnotations(testInstance.getClass(), testInstance);
50        processInjectMocks(testInstance.getClass(), testInstance);
51    }
52
53    private void processInjectMocks(final Class<?> clazz, final Object testInstance) {
54        Class<?> classContext = clazz;
55        while (classContext != Object.class) {
56            injectMocks(testInstance);
57            classContext = classContext.getSuperclass();
58        }
59    }
60
61    private void processIndependentAnnotations(final Class<?> clazz, final Object testInstance) {
62        Class<?> classContext = clazz;
63        while (classContext != Object.class) {
64            // this will create @Mocks, @Captors, etc:
65            delegate.process(classContext, testInstance);
66            // this will create @Spies:
67            spyAnnotationEngine.process(classContext, testInstance);
68
69            classContext = classContext.getSuperclass();
70        }
71    }
72
73}
74
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)