How to use method of org.mockito.internal.configuration.injection.MockInjection class

Best Mockito code snippet using org.mockito.internal.configuration.injection.MockInjection.

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/**
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.configuration;
6
7
8import java.lang.reflect.Field;
9import java.util.Set;
10import org.junit.Test;
11import org.mockito.internal.configuration.injection.MockInjection;
12
13
14@SuppressWarnings("unchecked")
15public class MockInjectionTest {
16    private MockInjectionTest.AnObjectWithConstructor withConstructor;
17
18    private MockInjectionTest.AnObjectWithoutConstructor withoutConstructor;
19
20    @Test(expected = IllegalArgumentException.class)
21    public void should_not_allow_null_on_field() {
22        MockInjection.onField(((Field) (null)), this);
23    }
24
25    @Test(expected = IllegalArgumentException.class)
26    public void should_not_allow_null_on_fields() {
27        MockInjection.onFields(((Set<Field>) (null)), this);
28    }
29
30    @Test(expected = IllegalArgumentException.class)
31    public void should_not_allow_null_on_instance_owning_the_field() throws Exception {
32        MockInjection.onField(field("withConstructor"), null);
33    }
34
35    @Test(expected = IllegalArgumentException.class)
36    public void should_not_allow_null_on_mocks() throws Exception {
37        MockInjection.onField(field("withConstructor"), this).withMocks(null);
38    }
39
40    @Test
41    public void can_try_constructor_injection() throws Exception {
42        MockInjection.onField(field("withConstructor"), this).withMocks(oneSetMock()).tryConstructorInjection().apply();
43        assertThat(withConstructor.initializedWithConstructor).isEqualTo(true);
44    }
45
46    @Test
47    public void should_not_fail_if_constructor_injection_is_not_possible() throws Exception {
48        MockInjection.onField(field("withoutConstructor"), this).withMocks(otherKindOfMocks()).tryConstructorInjection().apply();
49        assertThat(withoutConstructor).isNull();
50    }
51
52    @Test
53    public void can_try_property_or_setter_injection() throws Exception {
54        MockInjection.onField(field("withoutConstructor"), this).withMocks(oneSetMock()).tryPropertyOrFieldInjection().apply();
55        assertThat(withoutConstructor.theSet).isNotNull();
56    }
57
58    @Test
59    public void should_not_fail_if_property_or_field_injection_is_not_possible() throws Exception {
60        MockInjection.onField(field("withoutConstructor"), this).withMocks(otherKindOfMocks()).tryPropertyOrFieldInjection().apply();
61        assertThat(withoutConstructor.theSet).isNull();
62    }
63
64    public static class AnObjectWithConstructor {
65        public boolean initializedWithConstructor = false;
66
67        public AnObjectWithConstructor(Set<String> strings) {
68            initializedWithConstructor = true;
69        }
70    }
71
72    public static class AnObjectWithoutConstructor {
73        private Set<?> theSet;
74    }
75}
76
77
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)