From 0107b24976175bc6757deb26a2d72d67e5f63b3e Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Sun, 11 Oct 2015 22:06:00 -0300 Subject: [PATCH] Fix unit test by makinig getField use the field name directly --- .../LazyLoadedSingletonThreadSafetyTest.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java b/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java index a19e641f6..9b78f446f 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java @@ -73,20 +73,16 @@ public class LazyLoadedSingletonThreadSafetyTest { @Test @SuppressWarnings("unchecked") - public void test_HoleInSingletonCreationIfUsingReflection() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Field[] f = ThreadSafeLazyLoadedIvoryTower.class.getDeclaredFields(); - for (Field ff : f) { - System.out.println(ff.getDeclaringClass()); - } - assertEquals("One field only in ThreadSafeLazyLoadedIvoryTower", 1, f.length); - f[0].setAccessible(true); + public void test_HoleInSingletonCreationIfUsingReflection() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { + Field f = ThreadSafeLazyLoadedIvoryTower.class.getDeclaredField("instance"); + f.setAccessible(true); {//reflectively create an object - the singleton field is null Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower"); Constructor constructor = lazyIvoryTowerClazz.getDeclaredConstructor(); constructor.setAccessible(true); ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance(); - assertNull(f[0].get(instance)); + assertNull(f.get(instance)); } //instantiate the singleton but when we do the below code we are creating a new object where it is set to null still @@ -97,7 +93,7 @@ public class LazyLoadedSingletonThreadSafetyTest { Constructor constructor = lazyIvoryTowerClazz.getDeclaredConstructor(); constructor.setAccessible(true); ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance(); - assertNull(f[0].get(instance)); + assertNull(f.get(instance)); } }