Added test cases for accessing all data types.
This commit is contained in:
parent
a951d80b41
commit
885278e502
|
@ -4,7 +4,21 @@ public class Person {
|
|||
|
||||
private String name = "John";
|
||||
|
||||
private int age = 30;
|
||||
private byte age = 30;
|
||||
|
||||
private short uidNumber = 5555;
|
||||
|
||||
private int pinCode = 452002;
|
||||
|
||||
private long contactNumber = 123456789L;
|
||||
|
||||
private float height = 6.1242f;
|
||||
|
||||
private double weight = 75.2564;
|
||||
|
||||
private char gender = 'M';
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -14,20 +28,68 @@ public class Person {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
public byte getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
public void setAge(byte age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
private String greet(String name) {
|
||||
return "Hello " + name;
|
||||
public short getUidNumber() {
|
||||
return uidNumber;
|
||||
}
|
||||
|
||||
private int divideByZeroExample() {
|
||||
return 1 / 0;
|
||||
public void setUidNumber(short uidNumber) {
|
||||
this.uidNumber = uidNumber;
|
||||
}
|
||||
|
||||
public int getPinCode() {
|
||||
return pinCode;
|
||||
}
|
||||
|
||||
public void setPinCode(int pinCode) {
|
||||
this.pinCode = pinCode;
|
||||
}
|
||||
|
||||
public long getContactNumber() {
|
||||
return contactNumber;
|
||||
}
|
||||
|
||||
public void setContactNumber(long contactNumber) {
|
||||
this.contactNumber = contactNumber;
|
||||
}
|
||||
|
||||
public float getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(float height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(double weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public char getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(char gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,39 +11,112 @@ public class AccessPrivateFieldsUnitTest {
|
|||
public void whenGetFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
|
||||
Field fieldName = person.getClass()
|
||||
Field nameField = person.getClass()
|
||||
.getDeclaredField("name");
|
||||
fieldName.setAccessible(true);
|
||||
nameField.setAccessible(true);
|
||||
|
||||
String name = (String) fieldName.get(person);
|
||||
String name = (String) nameField.get(person);
|
||||
|
||||
Field fieldAge = person.getClass()
|
||||
Field ageField = person.getClass()
|
||||
.getDeclaredField("age");
|
||||
fieldAge.setAccessible(true);
|
||||
ageField.setAccessible(true);
|
||||
|
||||
int age = fieldAge.getInt(person);
|
||||
byte age = ageField.getByte(person);
|
||||
|
||||
Assertions.assertEquals("John", name);
|
||||
Assertions.assertEquals(30, age);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetIntegerFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
|
||||
Field uidNumberField = person.getClass()
|
||||
.getDeclaredField("uidNumber");
|
||||
uidNumberField.setAccessible(true);
|
||||
|
||||
short uidNumber = uidNumberField.getShort(person);
|
||||
|
||||
Field pinCodeField = person.getClass()
|
||||
.getDeclaredField("pinCode");
|
||||
pinCodeField.setAccessible(true);
|
||||
|
||||
int pinCode = pinCodeField.getInt(person);
|
||||
|
||||
Field contactNumberField = person.getClass()
|
||||
.getDeclaredField("contactNumber");
|
||||
contactNumberField.setAccessible(true);
|
||||
|
||||
long contactNumber = contactNumberField.getLong(person);
|
||||
|
||||
Assertions.assertEquals(5555, uidNumber);
|
||||
Assertions.assertEquals(452002, pinCode);
|
||||
Assertions.assertEquals(123456789L, contactNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFloatingTypeFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
|
||||
Field heightField = person.getClass()
|
||||
.getDeclaredField("height");
|
||||
heightField.setAccessible(true);
|
||||
|
||||
float height = heightField.getFloat(person);
|
||||
|
||||
Field weightField = person.getClass()
|
||||
.getDeclaredField("weight");
|
||||
weightField.setAccessible(true);
|
||||
|
||||
double weight = weightField.getDouble(person);
|
||||
|
||||
Assertions.assertEquals(6.1242f, height);
|
||||
Assertions.assertEquals(75.2564, weight);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetCharacterFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
|
||||
Field genderField = person.getClass()
|
||||
.getDeclaredField("gender");
|
||||
genderField.setAccessible(true);
|
||||
|
||||
char gender = genderField.getChar(person);
|
||||
|
||||
Assertions.assertEquals('M', gender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBooleanFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
|
||||
Field activeField = person.getClass()
|
||||
.getDeclaredField("active");
|
||||
activeField.setAccessible(true);
|
||||
|
||||
boolean active = activeField.getBoolean(person);
|
||||
|
||||
Assertions.assertTrue(active);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInt_whenGetStringField_thenIllegalArgumentException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
Field fieldName = person.getClass()
|
||||
Field nameField = person.getClass()
|
||||
.getDeclaredField("name");
|
||||
fieldName.setAccessible(true);
|
||||
nameField.setAccessible(true);
|
||||
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> fieldName.getInt(person));
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> nameField.getInt(person));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFieldNotSetAccessible_thenIllegalAccessException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
|
||||
Person person = new Person();
|
||||
Field fieldName = person.getClass()
|
||||
Field nameField = person.getClass()
|
||||
.getDeclaredField("name");
|
||||
|
||||
Assertions.assertThrows(IllegalAccessException.class, () -> fieldName.get(person));
|
||||
Assertions.assertThrows(IllegalAccessException.class, () -> nameField.get(person));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue