Segregated test cases for primitive and object types. Added test case

for Autoboxing.
This commit is contained in:
Umang Budhwar 2020-06-27 16:33:20 +05:30
parent 885278e502
commit 0e528b11d2
1 changed files with 29 additions and 21 deletions

View File

@ -8,50 +8,49 @@ import org.junit.jupiter.api.Test;
public class AccessPrivateFieldsUnitTest { public class AccessPrivateFieldsUnitTest {
@Test @Test
public void whenGetFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException { public void whenGetIntegerFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();
Field nameField = person.getClass()
.getDeclaredField("name");
nameField.setAccessible(true);
String name = (String) nameField.get(person);
Field ageField = person.getClass() Field ageField = person.getClass()
.getDeclaredField("age"); .getDeclaredField("age");
ageField.setAccessible(true); ageField.setAccessible(true);
byte age = ageField.getByte(person); byte age = ageField.getByte(person);
Assertions.assertEquals("John", name);
Assertions.assertEquals(30, age); Assertions.assertEquals(30, age);
}
@Test
public void whenGetIntegerFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person();
Field uidNumberField = person.getClass() Field uidNumberField = person.getClass()
.getDeclaredField("uidNumber"); .getDeclaredField("uidNumber");
uidNumberField.setAccessible(true); uidNumberField.setAccessible(true);
short uidNumber = uidNumberField.getShort(person); short uidNumber = uidNumberField.getShort(person);
Assertions.assertEquals(5555, uidNumber);
Field pinCodeField = person.getClass() Field pinCodeField = person.getClass()
.getDeclaredField("pinCode"); .getDeclaredField("pinCode");
pinCodeField.setAccessible(true); pinCodeField.setAccessible(true);
int pinCode = pinCodeField.getInt(person); int pinCode = pinCodeField.getInt(person);
Assertions.assertEquals(452002, pinCode);
Field contactNumberField = person.getClass() Field contactNumberField = person.getClass()
.getDeclaredField("contactNumber"); .getDeclaredField("contactNumber");
contactNumberField.setAccessible(true); contactNumberField.setAccessible(true);
long contactNumber = contactNumberField.getLong(person); long contactNumber = contactNumberField.getLong(person);
Assertions.assertEquals(5555, uidNumber);
Assertions.assertEquals(452002, pinCode);
Assertions.assertEquals(123456789L, contactNumber); Assertions.assertEquals(123456789L, contactNumber);
}
@Test
public void whenDoAutoboxing_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person();
Field pinCodeField = person.getClass()
.getDeclaredField("pinCode");
pinCodeField.setAccessible(true);
Integer pinCode = pinCodeField.getInt(person);
Assertions.assertEquals(452002, pinCode);
} }
@Test @Test
@ -63,14 +62,13 @@ public class AccessPrivateFieldsUnitTest {
heightField.setAccessible(true); heightField.setAccessible(true);
float height = heightField.getFloat(person); float height = heightField.getFloat(person);
Assertions.assertEquals(6.1242f, height);
Field weightField = person.getClass() Field weightField = person.getClass()
.getDeclaredField("weight"); .getDeclaredField("weight");
weightField.setAccessible(true); weightField.setAccessible(true);
double weight = weightField.getDouble(person); double weight = weightField.getDouble(person);
Assertions.assertEquals(6.1242f, height);
Assertions.assertEquals(75.2564, weight); Assertions.assertEquals(75.2564, weight);
} }
@ -83,7 +81,6 @@ public class AccessPrivateFieldsUnitTest {
genderField.setAccessible(true); genderField.setAccessible(true);
char gender = genderField.getChar(person); char gender = genderField.getChar(person);
Assertions.assertEquals('M', gender); Assertions.assertEquals('M', gender);
} }
@ -96,10 +93,21 @@ public class AccessPrivateFieldsUnitTest {
activeField.setAccessible(true); activeField.setAccessible(true);
boolean active = activeField.getBoolean(person); boolean active = activeField.getBoolean(person);
Assertions.assertTrue(active); Assertions.assertTrue(active);
} }
@Test
public void whenGetObjectFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person();
Field nameField = person.getClass()
.getDeclaredField("name");
nameField.setAccessible(true);
String name = (String) nameField.get(person);
Assertions.assertEquals("John", name);
}
@Test @Test
public void givenInt_whenGetStringField_thenIllegalArgumentException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException { public void givenInt_whenGetStringField_thenIllegalArgumentException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();