From 8e2725a64e604c7add45ca58203626a7238f9618 Mon Sep 17 00:00:00 2001 From: Umang Budhwar Date: Wed, 10 Jun 2020 23:27:42 +0530 Subject: [PATCH] Added unit test cases in givenX_whenY_thenZ format. --- .../access/privatefields/Person.java | 11 ++-- .../AccessPrivateFieldsUnitTest.java | 43 +++++++++------- .../AccessPrivateMethodsUnitTest.java | 51 ++++++++++++------- 3 files changed, 65 insertions(+), 40 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 e59ff3bea8..199d2429a7 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 @@ -1,7 +1,7 @@ package com.baeldung.reflection.access.privatefields; public class Person { - + private String name; public String getName() { @@ -12,7 +12,12 @@ public class Person { this.name = name; } - private String greet(String number) { - return "Hi " + Integer.parseInt(number); + private String greet(String name) { + return "Hello " + name; } + + private int divideByZeroExample() { + return 1 / 0; + } + } 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 8faea06f78..200ccea9e3 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 @@ -1,36 +1,43 @@ package com.baeldung.reflection.access.privatefields; +import java.lang.reflect.Field; + import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class AccessPrivateFieldsUnitTest { - private static AccessPrivateProperties accessPrivateProperties; + @Test + public void whenGetField_thenSuccess() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { + Person person = new Person(); + person.setName("John"); - @BeforeAll - public static void beforeAll() { - accessPrivateProperties = new AccessPrivateProperties(); + Field field = person.getClass() + .getDeclaredField("name"); + field.setAccessible(true); + + String name = (String) field.get(person); + + Assertions.assertEquals("John", name); } @Test - public void testForNoSuchFieldException() { - Assertions.assertThrows(NoSuchFieldException.class, () -> accessPrivateProperties.accessPrivateFields("firstName", "John", true)); + public void givenInt_whenSetStringField_thenIllegalArgumentException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { + Person person = new Person(); + Field field = person.getClass() + .getDeclaredField("name"); + field.setAccessible(true); + + Assertions.assertThrows(IllegalArgumentException.class, () -> field.setInt(person, 25)); } @Test - public void testForIllegalAccessException() { - Assertions.assertThrows(IllegalAccessException.class, () -> accessPrivateProperties.accessPrivateFields("name", "John", false)); - } + public void whenFieldNotSetAccessible_thenIllegalAccessException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException { + Person person = new Person(); + Field field = person.getClass() + .getDeclaredField("name"); - @Test - public void testForIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> accessPrivateProperties.accessPrivateFields("name", 25, true)); - } - - @Test - public void testForNullPointerException() { - Assertions.assertThrows(NullPointerException.class, () -> accessPrivateProperties.accessPrivateFields(null, "John", true)); + Assertions.assertThrows(IllegalAccessException.class, () -> field.set(person, "John")); } } 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 a2e0f3fe28..b44731c7ef 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 @@ -1,43 +1,56 @@ package com.baeldung.reflection.access.privatefields; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class AccessPrivateMethodsUnitTest { - private static AccessPrivateProperties accessPrivateProperties; + @Test + public void whenGetMethod_thenSuccess() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Person person = new Person(); - @BeforeAll - public static void beforeAll() { - accessPrivateProperties = new AccessPrivateProperties(); + Method method = person.getClass() + .getDeclaredMethod("greet", String.class); + method.setAccessible(true); + + String greetMessage = (String) method.invoke(person, "John"); + + Assertions.assertEquals("Hello John", greetMessage); } @Test - public void testForNoSuchMethodException() { - Assertions.assertThrows(NoSuchMethodException.class, () -> accessPrivateProperties.accessPrivateMethods("greeting", String.class, "5", true)); + public void givenInt_whenInvokeMethod_thenIllegalArgumentException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Person person = new Person(); + + Method method = person.getClass() + .getDeclaredMethod("greet", String.class); + method.setAccessible(true); + + Assertions.assertThrows(IllegalArgumentException.class, () -> method.invoke(person, 25)); } @Test - public void testForIllegalAccessException() { - Assertions.assertThrows(IllegalAccessException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, "5", false)); + public void whenMethodNotSetAccessible_thenIllegalAccessException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Person person = new Person(); + + Method method = person.getClass() + .getDeclaredMethod("greet", String.class); + + Assertions.assertThrows(IllegalAccessException.class, () -> method.invoke(person, "John")); } @Test - public void testForIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, 5, true)); - } + public void whenCallingMethodThrowsException_thenInvocationTargetException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Person person = new Person(); - @Test - public void testForNullPointerException() { - Assertions.assertThrows(NullPointerException.class, () -> accessPrivateProperties.accessPrivateMethods(null, String.class, "5", true)); - } + Method method = person.getClass() + .getDeclaredMethod("divideByZeroExample"); + method.setAccessible(true); - @Test - public void testForInvocationTargetException() { - Assertions.assertThrows(InvocationTargetException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, "John", true)); + Assertions.assertThrows(InvocationTargetException.class, () -> method.invoke(person)); } }