From f72691730627f9b54bf3ef38a516bff5643a250b Mon Sep 17 00:00:00 2001 From: Umang Budhwar Date: Mon, 15 Jun 2020 23:32:09 +0530 Subject: [PATCH] Included test cases for NoSuchFieldException, NoSuchMethodException and NullPointerException. --- .../access/privatefields/Person.java | 12 ++++- .../AccessPrivateFieldsUnitTest.java | 46 ++++++++++++++----- .../AccessPrivateMethodsUnitTest.java | 32 +++++++++---- 3 files changed, 68 insertions(+), 22 deletions(-) diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java index 199d2429a7..2ec263f6ae 100755 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java +++ b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java @@ -2,7 +2,9 @@ package com.baeldung.reflection.access.privatefields; public class Person { - private String name; + private String name = "John"; + + private int age = 30; public String getName() { return name; @@ -12,6 +14,14 @@ public class Person { this.name = name; } + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + private String greet(String name) { return "Hello " + name; } diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java index 200ccea9e3..b885e3b669 100644 --- a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java @@ -8,36 +8,58 @@ import org.junit.jupiter.api.Test; public class AccessPrivateFieldsUnitTest { @Test - public void whenGetField_thenSuccess() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { + public void whenGetFields_thenSuccess() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException { Person person = new Person(); - person.setName("John"); - Field field = person.getClass() + Field fieldName = person.getClass() .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(30, age); } @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(); - Field field = person.getClass() + Field fieldName = person.getClass() .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 - public void whenFieldNotSetAccessible_thenIllegalAccessException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { + public void whenFieldNotSetAccessible_thenIllegalAccessException() throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException, NullPointerException { Person person = new Person(); - Field field = person.getClass() + Field fieldName = person.getClass() .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)); } } diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java index b44731c7ef..10c953f4af 100644 --- a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java @@ -9,20 +9,20 @@ import org.junit.jupiter.api.Test; public class AccessPrivateMethodsUnitTest { @Test - public void whenGetMethod_thenSuccess() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + public void whenGetMethod_thenSuccess() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NullPointerException { Person person = new Person(); Method method = person.getClass() .getDeclaredMethod("greet", String.class); 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 - public void givenInt_whenInvokeMethod_thenIllegalArgumentException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + public void givenInt_whenInvokeMethod_thenIllegalArgumentException() throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, NullPointerException { Person person = new Person(); Method method = person.getClass() @@ -33,24 +33,38 @@ public class AccessPrivateMethodsUnitTest { } @Test - public void whenMethodNotSetAccessible_thenIllegalAccessException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + public void whenMethodNotSetAccessible_thenIllegalAccessException() throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, NullPointerException { Person person = new Person(); Method method = person.getClass() .getDeclaredMethod("greet", String.class); - Assertions.assertThrows(IllegalAccessException.class, () -> method.invoke(person, "John")); + Assertions.assertThrows(IllegalAccessException.class, () -> method.invoke(person, "Jane")); } @Test - public void whenCallingMethodThrowsException_thenInvocationTargetException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + public void whenCallingMethodThrowsException_thenInvocationTargetException() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NullPointerException { Person person = new Person(); - Method method = person.getClass() .getDeclaredMethod("divideByZeroExample"); method.setAccessible(true); - 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)); + } + }