Best Mockito code snippet using org.mockito.internal.util.reflection.FieldInitializerTest.ConcreteStaticClass
Source:FieldInitializerTest.java
...20 private FieldInitializerTest.StaticClassThrowingExceptionDefaultConstructor throwingExDefaultConstructor;21 private FieldInitializerTest.AbstractStaticClass abstractType;22 private FieldInitializerTest.Interface interfaceType;23 private FieldInitializerTest.InnerClassType innerClassType;24 private FieldInitializerTest.AbstractStaticClass instantiatedAbstractType = new FieldInitializerTest.ConcreteStaticClass();25 private FieldInitializerTest.Interface instantiatedInterfaceType = new FieldInitializerTest.ConcreteStaticClass();26 private FieldInitializerTest.InnerClassType instantiatedInnerClassType = new FieldInitializerTest.InnerClassType();27 @Test28 public void should_keep_same_instance_if_field_initialized() throws Exception {29 final FieldInitializerTest.StaticClass backupInstance = alreadyInstantiated;30 FieldInitializer fieldInitializer = new FieldInitializer(this, field("alreadyInstantiated"));31 FieldInitializationReport report = fieldInitializer.initialize();32 Assert.assertSame(backupInstance, report.fieldInstance());33 Assert.assertFalse(report.fieldWasInitialized());34 Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());35 }36 @Test37 public void should_instantiate_field_when_type_has_no_constructor() throws Exception {38 FieldInitializer fieldInitializer = new FieldInitializer(this, field("noConstructor"));39 FieldInitializationReport report = fieldInitializer.initialize();40 Assert.assertNotNull(report.fieldInstance());41 Assert.assertTrue(report.fieldWasInitialized());42 Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());43 }44 @Test45 public void should_instantiate_field_with_default_constructor() throws Exception {46 FieldInitializer fieldInitializer = new FieldInitializer(this, field("defaultConstructor"));47 FieldInitializationReport report = fieldInitializer.initialize();48 Assert.assertNotNull(report.fieldInstance());49 Assert.assertTrue(report.fieldWasInitialized());50 Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());51 }52 @Test53 public void should_instantiate_field_with_private_default_constructor() throws Exception {54 FieldInitializer fieldInitializer = new FieldInitializer(this, field("privateDefaultConstructor"));55 FieldInitializationReport report = fieldInitializer.initialize();56 Assert.assertNotNull(report.fieldInstance());57 Assert.assertTrue(report.fieldWasInitialized());58 Assert.assertFalse(report.fieldWasInitializedUsingContructorArgs());59 }60 @Test(expected = MockitoException.class)61 public void should_fail_to_instantiate_field_if_no_default_constructor() throws Exception {62 FieldInitializer fieldInitializer = new FieldInitializer(this, field("noDefaultConstructor"));63 fieldInitializer.initialize();64 }65 @Test66 public void should_fail_to_instantiate_field_if_default_constructor_throws_exception() throws Exception {67 FieldInitializer fieldInitializer = new FieldInitializer(this, field("throwingExDefaultConstructor"));68 try {69 fieldInitializer.initialize();70 Assert.fail();71 } catch (MockitoException e) {72 InvocationTargetException ite = ((InvocationTargetException) (e.getCause()));73 Assert.assertTrue(((ite.getTargetException()) instanceof NullPointerException));74 Assert.assertEquals("business logic failed", ite.getTargetException().getMessage());75 }76 }77 @Test(expected = MockitoException.class)78 public void should_fail_for_abstract_field() throws Exception {79 new FieldInitializer(this, field("abstractType"));80 }81 @Test82 public void should_not_fail_if_abstract_field_is_instantiated() throws Exception {83 new FieldInitializer(this, field("instantiatedAbstractType"));84 }85 @Test(expected = MockitoException.class)86 public void should_fail_for_interface_field() throws Exception {87 new FieldInitializer(this, field("interfaceType"));88 }89 @Test90 public void should_not_fail_if_interface_field_is_instantiated() throws Exception {91 new FieldInitializer(this, field("instantiatedInterfaceType"));92 }93 @Test(expected = MockitoException.class)94 public void should_fail_for_local_type_field() throws Exception {95 // when96 class LocalType {}97 class TheTestWithLocalType {98 @InjectMocks99 LocalType field;100 }101 TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();102 // when103 new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));104 }105 @Test106 public void should_not_fail_if_local_type_field_is_instantiated() throws Exception {107 // when108 class LocalType {}109 class TheTestWithLocalType {110 @InjectMocks111 LocalType field = new LocalType();112 }113 TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();114 // when115 new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));116 }117 @Test(expected = MockitoException.class)118 public void should_fail_for_inner_class_field() throws Exception {119 new FieldInitializer(this, field("innerClassType"));120 }121 @Test122 public void should_not_fail_if_inner_class_field_is_instantiated() throws Exception {123 new FieldInitializer(this, field("instantiatedInnerClassType"));124 }125 @Test126 public void can_instantiate_class_with_parameterized_constructor() throws Exception {127 FieldInitializer.ConstructorArgumentResolver resolver = BDDMockito.given(Mockito.mock(FieldInitializer.ConstructorArgumentResolver.class).resolveTypeInstances(ArgumentMatchers.any(Class.class))).willReturn(new Object[]{ null }).getMock();128 new FieldInitializer(this, field("noDefaultConstructor"), resolver).initialize();129 Assert.assertNotNull(noDefaultConstructor);130 }131 static class StaticClass {}132 static class StaticClassWithDefaultConstructor {133 StaticClassWithDefaultConstructor() {134 }135 }136 static class StaticClassWithPrivateDefaultConstructor {137 private StaticClassWithPrivateDefaultConstructor() {138 }139 }140 static class StaticClassWithoutDefaultConstructor {141 private StaticClassWithoutDefaultConstructor(String param) {142 }143 }144 static class StaticClassThrowingExceptionDefaultConstructor {145 StaticClassThrowingExceptionDefaultConstructor() throws Exception {146 throw new NullPointerException("business logic failed");147 }148 }149 abstract static class AbstractStaticClass {150 public AbstractStaticClass() {151 }152 }153 interface Interface {}154 static class ConcreteStaticClass extends FieldInitializerTest.AbstractStaticClass implements FieldInitializerTest.Interface {}155 class InnerClassType {156 InnerClassType() {157 }158 }159}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!