Add additional unit tests to show how lazy loading is working with reflection

This commit is contained in:
Richard Jones 2015-10-11 21:58:13 -03:00
parent 45b0ac386e
commit 7cf5b32086

View File

@ -0,0 +1,41 @@
package com.iluwatar.lazy.loading;
import org.junit.Test;
import java.lang.reflect.Field;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* Using reflection this test shows that the heavy field is not instantiated until the method getHeavy is called
*
* Created by jones on 11/10/2015.
*/
public class HolderThreadSafeTest {
@Test
public void test() throws IllegalAccessException {
HolderThreadSafe hts = new HolderThreadSafe();
{//first call is null
Field[] f = HolderThreadSafe.class.getDeclaredFields();
assertEquals("One field only in HolderThreadSafe", 1, f.length);
f[0].setAccessible(true);
assertNull(f[0].get(hts));
}
// now it is lazily loaded
hts.getHeavy();
{//now it is not null - call via reflection so that the test is the same before and after
Field[] f = HolderThreadSafe.class.getDeclaredFields();
assertEquals("One field only in HolderThreadSafe", 1, f.length);
f[0].setAccessible(true);
assertNotNull(f[0].get(hts));
}
}
}