Included test cases for NoSuchFieldException, NoSuchMethodException and

NullPointerException.
This commit is contained in:
Umang Budhwar 2020-06-15 23:32:09 +05:30
parent 8e2725a64e
commit f726917306
3 changed files with 68 additions and 22 deletions

View File

@ -2,7 +2,9 @@ package com.baeldung.reflection.access.privatefields;
public class Person { public class Person {
private String name; private String name = "John";
private int age = 30;
public String getName() { public String getName() {
return name; return name;
@ -12,6 +14,14 @@ public class Person {
this.name = name; this.name = name;
} }
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String greet(String name) { private String greet(String name) {
return "Hello " + name; return "Hello " + name;
} }

View File

@ -8,36 +8,58 @@ import org.junit.jupiter.api.Test;
public class AccessPrivateFieldsUnitTest { public class AccessPrivateFieldsUnitTest {
@Test @Test
public void whenGetField_thenSuccess() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { public void whenGetFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();
person.setName("John");
Field field = person.getClass() Field fieldName = person.getClass()
.getDeclaredField("name"); .getDeclaredField("name");
field.setAccessible(true); fieldName.setAccessible(true);
String name = (String) field.get(person); String name = (String) fieldName.get(person);
Field fieldAge = person.getClass()
.getDeclaredField("age");
fieldAge.setAccessible(true);
int age = fieldAge.getInt(person);
Assertions.assertEquals("John", name); Assertions.assertEquals("John", name);
Assertions.assertEquals(30, age);
} }
@Test @Test
public void givenInt_whenSetStringField_thenIllegalArgumentException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { public void givenInt_whenSetStringField_thenIllegalArgumentException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();
Field field = person.getClass() Field fieldName = person.getClass()
.getDeclaredField("name"); .getDeclaredField("name");
field.setAccessible(true); fieldName.setAccessible(true);
Assertions.assertThrows(IllegalArgumentException.class, () -> field.setInt(person, 25)); Assertions.assertThrows(IllegalArgumentException.class, () -> fieldName.setInt(person, 25));
} }
@Test @Test
public void whenFieldNotSetAccessible_thenIllegalAccessException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { public void whenFieldNotSetAccessible_thenIllegalAccessException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException {
Person person = new Person(); Person person = new Person();
Field field = person.getClass() Field fieldName = person.getClass()
.getDeclaredField("name"); .getDeclaredField("name");
Assertions.assertThrows(IllegalAccessException.class, () -> field.set(person, "John")); Assertions.assertThrows(IllegalAccessException.class, () -> fieldName.get(person));
}
@Test
public void whenAccessingWrongProperty_thenNoSuchFieldException() throws NoSuchFieldException, NullPointerException {
Person person = new Person();
Assertions.assertThrows(NoSuchFieldException.class, () -> person.getClass()
.getDeclaredField("genders"));
}
@Test
public void whenAccessingNullProperty_thenNullPointerException() throws NoSuchFieldException, NullPointerException {
Person person = new Person();
Assertions.assertThrows(NullPointerException.class, () -> person.getClass()
.getDeclaredField(null));
} }
} }

View File

@ -9,20 +9,20 @@ import org.junit.jupiter.api.Test;
public class AccessPrivateMethodsUnitTest { public class AccessPrivateMethodsUnitTest {
@Test @Test
public void whenGetMethod_thenSuccess() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void whenGetMethod_thenSuccess() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NullPointerException {
Person person = new Person(); Person person = new Person();
Method method = person.getClass() Method method = person.getClass()
.getDeclaredMethod("greet", String.class); .getDeclaredMethod("greet", String.class);
method.setAccessible(true); method.setAccessible(true);
String greetMessage = (String) method.invoke(person, "John"); String greetMessage = (String) method.invoke(person, "Jane");
Assertions.assertEquals("Hello John", greetMessage); Assertions.assertEquals("Hello Jane", greetMessage);
} }
@Test @Test
public void givenInt_whenInvokeMethod_thenIllegalArgumentException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void givenInt_whenInvokeMethod_thenIllegalArgumentException() throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, NullPointerException {
Person person = new Person(); Person person = new Person();
Method method = person.getClass() Method method = person.getClass()
@ -33,24 +33,38 @@ public class AccessPrivateMethodsUnitTest {
} }
@Test @Test
public void whenMethodNotSetAccessible_thenIllegalAccessException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void whenMethodNotSetAccessible_thenIllegalAccessException() throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, NullPointerException {
Person person = new Person(); Person person = new Person();
Method method = person.getClass() Method method = person.getClass()
.getDeclaredMethod("greet", String.class); .getDeclaredMethod("greet", String.class);
Assertions.assertThrows(IllegalAccessException.class, () -> method.invoke(person, "John")); Assertions.assertThrows(IllegalAccessException.class, () -> method.invoke(person, "Jane"));
} }
@Test @Test
public void whenCallingMethodThrowsException_thenInvocationTargetException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void whenCallingMethodThrowsException_thenInvocationTargetException() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NullPointerException {
Person person = new Person(); Person person = new Person();
Method method = person.getClass() Method method = person.getClass()
.getDeclaredMethod("divideByZeroExample"); .getDeclaredMethod("divideByZeroExample");
method.setAccessible(true); method.setAccessible(true);
Assertions.assertThrows(InvocationTargetException.class, () -> method.invoke(person)); Assertions.assertThrows(InvocationTargetException.class, () -> method.invoke(person));
} }
@Test
public void whenAccessingWrongProperty_thenNoSuchMethodException() throws NoSuchMethodException, NullPointerException {
Person person = new Person();
Assertions.assertThrows(NoSuchMethodException.class, () -> person.getClass()
.getDeclaredMethod("greeting", String.class));
}
@Test
public void whenAccessingNullMethod_thenNullPointerException() throws NoSuchMethodException, NullPointerException {
Person person = new Person();
Assertions.assertThrows(NullPointerException.class, () -> person.getClass()
.getDeclaredMethod(null, String.class));
}
} }