Added unit test cases in givenX_whenY_thenZ format.

This commit is contained in:
Umang Budhwar 2020-06-10 23:27:42 +05:30
parent ba3bd78a10
commit 8e2725a64e
3 changed files with 65 additions and 40 deletions

View File

@ -1,7 +1,7 @@
package com.baeldung.reflection.access.privatefields; package com.baeldung.reflection.access.privatefields;
public class Person { public class Person {
private String name; private String name;
public String getName() { public String getName() {
@ -12,7 +12,12 @@ public class Person {
this.name = name; this.name = name;
} }
private String greet(String number) { private String greet(String name) {
return "Hi " + Integer.parseInt(number); return "Hello " + name;
} }
private int divideByZeroExample() {
return 1 / 0;
}
} }

View File

@ -1,36 +1,43 @@
package com.baeldung.reflection.access.privatefields; package com.baeldung.reflection.access.privatefields;
import java.lang.reflect.Field;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class AccessPrivateFieldsUnitTest { 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 Field field = person.getClass()
public static void beforeAll() { .getDeclaredField("name");
accessPrivateProperties = new AccessPrivateProperties(); field.setAccessible(true);
String name = (String) field.get(person);
Assertions.assertEquals("John", name);
} }
@Test @Test
public void testForNoSuchFieldException() { public void givenInt_whenSetStringField_thenIllegalArgumentException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException {
Assertions.assertThrows(NoSuchFieldException.class, () -> accessPrivateProperties.accessPrivateFields("firstName", "John", true)); Person person = new Person();
Field field = person.getClass()
.getDeclaredField("name");
field.setAccessible(true);
Assertions.assertThrows(IllegalArgumentException.class, () -> field.setInt(person, 25));
} }
@Test @Test
public void testForIllegalAccessException() { public void whenFieldNotSetAccessible_thenIllegalAccessException() throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException {
Assertions.assertThrows(IllegalAccessException.class, () -> accessPrivateProperties.accessPrivateFields("name", "John", false)); Person person = new Person();
} Field field = person.getClass()
.getDeclaredField("name");
@Test Assertions.assertThrows(IllegalAccessException.class, () -> field.set(person, "John"));
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));
} }
} }

View File

@ -1,43 +1,56 @@
package com.baeldung.reflection.access.privatefields; package com.baeldung.reflection.access.privatefields;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class AccessPrivateMethodsUnitTest { public class AccessPrivateMethodsUnitTest {
private static AccessPrivateProperties accessPrivateProperties; @Test
public void whenGetMethod_thenSuccess() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Person person = new Person();
@BeforeAll Method method = person.getClass()
public static void beforeAll() { .getDeclaredMethod("greet", String.class);
accessPrivateProperties = new AccessPrivateProperties(); method.setAccessible(true);
String greetMessage = (String) method.invoke(person, "John");
Assertions.assertEquals("Hello John", greetMessage);
} }
@Test @Test
public void testForNoSuchMethodException() { public void givenInt_whenInvokeMethod_thenIllegalArgumentException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Assertions.assertThrows(NoSuchMethodException.class, () -> accessPrivateProperties.accessPrivateMethods("greeting", String.class, "5", true)); Person person = new Person();
Method method = person.getClass()
.getDeclaredMethod("greet", String.class);
method.setAccessible(true);
Assertions.assertThrows(IllegalArgumentException.class, () -> method.invoke(person, 25));
} }
@Test @Test
public void testForIllegalAccessException() { public void whenMethodNotSetAccessible_thenIllegalAccessException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Assertions.assertThrows(IllegalAccessException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, "5", false)); Person person = new Person();
Method method = person.getClass()
.getDeclaredMethod("greet", String.class);
Assertions.assertThrows(IllegalAccessException.class, () -> method.invoke(person, "John"));
} }
@Test @Test
public void testForIllegalArgumentException() { public void whenCallingMethodThrowsException_thenInvocationTargetException() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Assertions.assertThrows(IllegalArgumentException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, 5, true)); Person person = new Person();
}
@Test Method method = person.getClass()
public void testForNullPointerException() { .getDeclaredMethod("divideByZeroExample");
Assertions.assertThrows(NullPointerException.class, () -> accessPrivateProperties.accessPrivateMethods(null, String.class, "5", true)); method.setAccessible(true);
}
@Test Assertions.assertThrows(InvocationTargetException.class, () -> method.invoke(person));
public void testForInvocationTargetException() {
Assertions.assertThrows(InvocationTargetException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, "John", true));
} }
} }