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;
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;
}
}

View File

@ -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"));
}
}

View File

@ -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));
}
}