BAEL-4399:Changes for showing NoSuchFieldError

This commit is contained in:
Amitabh Tiwari 2020-10-23 21:36:49 +05:30
parent 7f19907f51
commit 71daa1a7c1
4 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package main.java.oldclass;
public class Dependent {
//This needed to be commented post compilation of NoSuchFielDError and Compile
public static String message = "Hello Baeldung!!";
}

View File

@ -0,0 +1,7 @@
package main.java.oldclass;
public class NoSuchFieldError {
public static void main(String[] args) {
System.out.println(Dependent.message);
}
}

View File

@ -0,0 +1,20 @@
package main.java.reflection;
import java.lang.reflect.Field;
public class NoSuchFieldError2 {
//String message = "Hello Baeldung!!!";
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
print();
}
public static void print(){
ValidateNoSuchFieldError clss = new ValidateNoSuchFieldError();
Class<?> aClass = Class.forName(clss.getClass().getName());
Field field = aClass.getDeclaredField("message");
field.setAccessible(true);
String msgStr = (String)field.get(clss);
System.out.println(msgStr);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.exceptions.nosuchmethoderror;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
public class NoSuchFieldErrorTest {
@Test(expected = NoSuchFieldException.class)
public void whenFieldNotFound_thenThrowNoSuchFieldException() {
NoSuchFieldError2.print();
}
}