How to use FieldInitializerTest class of org.mockito.internal.util.reflection package

Best Mockito code snippet using org.mockito.internal.util.reflection.FieldInitializerTest

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.util.reflection;
6
7
8import java.lang.reflect.InvocationTargetException;
9import org.junit.Assert;
10import org.junit.Test;
11import org.mockito.ArgumentMatchers;
12import org.mockito.BDDMockito;
13import org.mockito.InjectMocks;
14import org.mockito.Mockito;
15import org.mockito.exceptions.base.MockitoException;
16
17
18public class FieldInitializerTest {
19    private FieldInitializerTest.StaticClass alreadyInstantiated = new FieldInitializerTest.StaticClass();
20
21    private FieldInitializerTest.StaticClass noConstructor;
22
23    private FieldInitializerTest.StaticClassWithDefaultConstructor defaultConstructor;
24
25    private FieldInitializerTest.StaticClassWithPrivateDefaultConstructor privateDefaultConstructor;
26
27    private FieldInitializerTest.StaticClassWithoutDefaultConstructor noDefaultConstructor;
28
29    private FieldInitializerTest.StaticClassThrowingExceptionDefaultConstructor throwingExDefaultConstructor;
30
31    private FieldInitializerTest.AbstractStaticClass abstractType;
32
33    private FieldInitializerTest.Interface interfaceType;
34
35    private FieldInitializerTest.InnerClassType innerClassType;
36
37    private FieldInitializerTest.AbstractStaticClass instantiatedAbstractType = new FieldInitializerTest.ConcreteStaticClass();
38
39    private FieldInitializerTest.Interface instantiatedInterfaceType = new FieldInitializerTest.ConcreteStaticClass();
40
41    private FieldInitializerTest.InnerClassType instantiatedInnerClassType = new FieldInitializerTest.InnerClassType();
42
43    @Test
44    public void should_keep_same_instance_if_field_initialized() throws Exception {
45        final FieldInitializerTest.StaticClass backupInstance = alreadyInstantiated;
46        FieldInitializer fieldInitializer = new FieldInitializer(this, field("alreadyInstantiated"));
47        FieldInitializationReport report = fieldInitializer.initialize();
48        Assert.assertSame(backupInstance, report.fieldInstance());
49        Assert.assertFalse(report.fieldWasInitialized());
50        Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());
51    }
52
53    @Test
54    public void should_instantiate_field_when_type_has_no_constructor() throws Exception {
55        FieldInitializer fieldInitializer = new FieldInitializer(this, field("noConstructor"));
56        FieldInitializationReport report = fieldInitializer.initialize();
57        Assert.assertNotNull(report.fieldInstance());
58        Assert.assertTrue(report.fieldWasInitialized());
59        Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());
60    }
61
62    @Test
63    public void should_instantiate_field_with_default_constructor() throws Exception {
64        FieldInitializer fieldInitializer = new FieldInitializer(this, field("defaultConstructor"));
65        FieldInitializationReport report = fieldInitializer.initialize();
66        Assert.assertNotNull(report.fieldInstance());
67        Assert.assertTrue(report.fieldWasInitialized());
68        Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());
69    }
70
71    @Test
72    public void should_instantiate_field_with_private_default_constructor() throws Exception {
73        FieldInitializer fieldInitializer = new FieldInitializer(this, field("privateDefaultConstructor"));
74        FieldInitializationReport report = fieldInitializer.initialize();
75        Assert.assertNotNull(report.fieldInstance());
76        Assert.assertTrue(report.fieldWasInitialized());
77        Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());
78    }
79
80    @Test(expected = MockitoException.class)
81    public void should_fail_to_instantiate_field_if_no_default_constructor() throws Exception {
82        FieldInitializer fieldInitializer = new FieldInitializer(this, field("noDefaultConstructor"));
83        fieldInitializer.initialize();
84    }
85
86    @Test
87    public void should_fail_to_instantiate_field_if_default_constructor_throws_exception() throws Exception {
88        FieldInitializer fieldInitializer = new FieldInitializer(this, field("throwingExDefaultConstructor"));
89        try {
90            fieldInitializer.initialize();
91            Assert.fail();
92        } catch (MockitoException e) {
93            InvocationTargetException ite = ((InvocationTargetException) (e.getCause()));
94            Assert.assertTrue(((ite.getTargetException()) instanceof NullPointerException));
95            Assert.assertEquals("business logic failed", ite.getTargetException().getMessage());
96        }
97    }
98
99    @Test(expected = MockitoException.class)
100    public void should_fail_for_abstract_field() throws Exception {
101        new FieldInitializer(this, field("abstractType"));
102    }
103
104    @Test
105    public void should_not_fail_if_abstract_field_is_instantiated() throws Exception {
106        new FieldInitializer(this, field("instantiatedAbstractType"));
107    }
108
109    @Test(expected = MockitoException.class)
110    public void should_fail_for_interface_field() throws Exception {
111        new FieldInitializer(this, field("interfaceType"));
112    }
113
114    @Test
115    public void should_not_fail_if_interface_field_is_instantiated() throws Exception {
116        new FieldInitializer(this, field("instantiatedInterfaceType"));
117    }
118
119    @Test(expected = MockitoException.class)
120    public void should_fail_for_local_type_field() throws Exception {
121        // when
122        class LocalType {}
123        class TheTestWithLocalType {
124            @InjectMocks
125            LocalType field;
126        }
127        TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();
128        // when
129        new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));
130    }
131
132    @Test
133    public void should_not_fail_if_local_type_field_is_instantiated() throws Exception {
134        // when
135        class LocalType {}
136        class TheTestWithLocalType {
137            @InjectMocks
138            LocalType field = new LocalType();
139        }
140        TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();
141        // when
142        new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));
143    }
144
145    @Test(expected = MockitoException.class)
146    public void should_fail_for_inner_class_field() throws Exception {
147        new FieldInitializer(this, field("innerClassType"));
148    }
149
150    @Test
151    public void should_not_fail_if_inner_class_field_is_instantiated() throws Exception {
152        new FieldInitializer(this, field("instantiatedInnerClassType"));
153    }
154
155    @Test
156    public void can_instantiate_class_with_parameterized_constructor() throws Exception {
157        FieldInitializer.ConstructorArgumentResolver resolver = BDDMockito.given(Mockito.mock(FieldInitializer.ConstructorArgumentResolver.class).resolveTypeInstances(ArgumentMatchers.any(Class.class))).willReturn(new Object[]{ null }).getMock();
158        new FieldInitializer(this, field("noDefaultConstructor"), resolver).initialize();
159        Assert.assertNotNull(noDefaultConstructor);
160    }
161
162    static class StaticClass {}
163
164    static class StaticClassWithDefaultConstructor {
165        StaticClassWithDefaultConstructor() {
166        }
167    }
168
169    static class StaticClassWithPrivateDefaultConstructor {
170        private StaticClassWithPrivateDefaultConstructor() {
171        }
172    }
173
174    static class StaticClassWithoutDefaultConstructor {
175        private StaticClassWithoutDefaultConstructor(String param) {
176        }
177    }
178
179    static class StaticClassThrowingExceptionDefaultConstructor {
180        StaticClassThrowingExceptionDefaultConstructor() throws Exception {
181            throw new NullPointerException("business logic failed");
182        }
183    }
184
185    abstract static class AbstractStaticClass {
186        public AbstractStaticClass() {
187        }
188    }
189
190    interface Interface {}
191
192    static class ConcreteStaticClass extends FieldInitializerTest.AbstractStaticClass implements FieldInitializerTest.Interface {}
193
194    class InnerClassType {
195        InnerClassType() {
196        }
197    }
198}
199
200
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
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)