Added test case for widening.

This commit is contained in:
Umang Budhwar 2020-06-29 22:27:35 +05:30
parent 5236d6cedd
commit 0549f755b0
1 changed files with 23 additions and 1 deletions

View File

@ -53,6 +53,18 @@ public class AccessPrivateFieldsUnitTest {
Assertions.assertEquals(452002, pinCode); Assertions.assertEquals(452002, pinCode);
} }
@Test
public void whenDoWidening_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person();
Field pinCodeField = person.getClass()
.getDeclaredField("pinCode");
pinCodeField.setAccessible(true);
Long pinCode = pinCodeField.getLong(person);
Assertions.assertEquals(452002L, pinCode);
}
@Test @Test
public void whenGetFloatingTypeFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException { public void whenGetFloatingTypeFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();
@ -118,6 +130,16 @@ public class AccessPrivateFieldsUnitTest {
Assertions.assertThrows(IllegalArgumentException.class, () -> nameField.getInt(person)); Assertions.assertThrows(IllegalArgumentException.class, () -> nameField.getInt(person));
} }
@Test
public void givenInt_whenGetLongField_thenIllegalArgumentException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person();
Field contactNumberField = person.getClass()
.getDeclaredField("contactNumber");
contactNumberField.setAccessible(true);
Assertions.assertThrows(IllegalArgumentException.class, () -> contactNumberField.getInt(person));
}
@Test @Test
public void whenFieldNotSetAccessible_thenIllegalAccessException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException { public void whenFieldNotSetAccessible_thenIllegalAccessException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();
@ -132,7 +154,7 @@ public class AccessPrivateFieldsUnitTest {
Person person = new Person(); Person person = new Person();
Assertions.assertThrows(NoSuchFieldException.class, () -> person.getClass() Assertions.assertThrows(NoSuchFieldException.class, () -> person.getClass()
.getDeclaredField("genders")); .getDeclaredField("firstName"));
} }
@Test @Test