BAEL-4399: Added changes as per review comment
This commit is contained in:
parent
dcc5d77c1b
commit
819669c97c
@ -1,6 +1,6 @@
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
public class Dependent {
|
||||
// This needed to be commented post compilation of NoSuchFielDError and Compile
|
||||
public static String message = "Hello Baeldung!!";
|
||||
// This needed to be commented post compilation of NoSuchFielDError and Compile
|
||||
public static String message = "Hello Baeldung!!";
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class NoSuchFieldError {
|
||||
// String message = "Hello Baeldung!!!";
|
||||
|
||||
public static String getMessage() throws ClassNotFoundException, NoSuchFieldException, SecurityException,
|
||||
IllegalArgumentException, IllegalAccessException {
|
||||
NoSuchFieldError clss = new NoSuchFieldError();
|
||||
Class<?> aClass = Class.forName(clss.getClass().getName());
|
||||
Field field = aClass.getDeclaredField("message");
|
||||
field.setAccessible(true);
|
||||
String msgStr = (String) field.get(clss);
|
||||
return msgStr;
|
||||
}
|
||||
|
||||
public static String getDependentMessage() {
|
||||
return Dependent.message;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
public class NoSuchFieldErrorExample {
|
||||
|
||||
public static String getDependentMessage() {
|
||||
return Dependent.message;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NoSuchFieldErrorExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDependentMessage_returnMessage() {
|
||||
String dependentMessage = NoSuchFieldErrorExample.getDependentMessage();
|
||||
assertTrue("Hello Baeldung".equals(dependentMessage));
|
||||
}
|
||||
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.baeldung.exceptions.nosuchfielderror;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NoSuchFieldErrorTest {
|
||||
|
||||
@Test(expected = NoSuchFieldException.class)
|
||||
public void whenFieldNotFound_thenThrowNoSuchFieldException() throws Exception {
|
||||
NoSuchFieldError.getMessage();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchFieldException.class)
|
||||
public void whenDependentFieldNotFound_thenThrowNoSuchFieldException() {
|
||||
NoSuchFieldError.getDependentMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentFieldNotFound_returnMessage() {
|
||||
String dependentMessage = NoSuchFieldError.getDependentMessage();
|
||||
assertTrue("Hello Baeldung".equals(dependentMessage));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user