I am trying to write test to ensure whether I am calling the enum's method or not but getting error :
@RunWith(PowerMockRunner.class)
@PrepareForTest({MonitorTask.class})
public class MonitorTest {
@Test
public void initialize() {
MonitorTask mockInstance = mock(MonitorTask.class);
Whitebox.setInternalState(MonitorTask.class, "TIMER", mockInstance);
Mockito.spy(mockInstance.initialize());
}
}
This is the class I am trying to test
public class Monitor {
public boolean initialize() {
return MonitorTask.TIMER.initialize();
}
}
The ENUM :
public enum MonitorTask {
TIMER;
private final AtomicBoolean isPublishing = new AtomicBoolean(false);
private final long period = ConfigUtils
.requireLong("getMonitor");
public synchronized boolean initialize() {
return initialize(period, period);
}
boolean initialize(long delay, long period) {
if (isPublishing.get()) {
return false;
}
TimerTask task = new TimerTask() {
@Override public void run() {
try {
Publisher.INSTANCE.update(DateTime.now());
} catch (Exception e) {
log.warn("Exception", e);
}
}
};
Timer timer = new Timer("MonitorTask", true);
timer.schedule(task, delay, period);
isPublishing.set(true);
return true;
}
}
Can some one tell me is there anything wrong with my test ? The Error I am getting :
java.lang.ExceptionInInitializerError at sun.reflect.GeneratedSerializationConstructorAccessor8.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45) at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73) at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(Unknown Source) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(Unknown Source) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(Unknown Source) at org.mockito.internal.creation.CglibMockMaker.createMock(Unknown Source) at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:43) at org.mockito.internal.util.MockUtil.createMock(Unknown Source) at org.mockito.internal.MockitoCore.mock(Unknown Source) at org.mockito.Mockito.mock(Unknown Source) at org.mockito.Mockito.mock(Unknown Source)