Although the link that you have provided doesn't show source code but check this blog for a few examples
@RunWith(PowerMockRunner.class)
@PrepareForTest({ReplicationAction.class})
public class SampleListenerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleListenerTest.class);
private SampleListener sampleListener = new SampleListener();
private Map<String, Object> propertyMap;
@Mock
private Event event;
@Mock
private ReplicationAction replicationAction;
@Mock
private JobManager jobManager;
@Before
public void before() {
setPropertyMap(SOMETHING);
MockitoAnnotations.initMocks(this);
// Needed to override static final methods
PowerMockito.mockStatic(ReplicationAction.class);
// mock private fields
try {
Field jobManagerField = SampleListener.class.getDeclaredField("jobManager");
jobManagerField.setAccessible(true);
jobManagerField.set(sampleListener, jobManager);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
when(ReplicationAction.fromEvent(any(Event.class))).thenReturn(replicationAction);
when(replicationAction.getPath()).thenReturn("/path");
when(replicationAction.getType()).thenReturn(ReplicationActionType.ACTIVATE);
}
private void setPropertyMap(String key) {
propertyMap = new HashedMap();
String[] values = new String[] {"/path"};
propertyMap.put(key, values);
}
@Test
public void testActivate() {
sampleListener.activate(propertyMap);
}
@Test
public void testUpdate() {
sampleListener.update(propertyMap);
}
@Test
public void testSampleHandleEvent() {
setPropertyMap(SOMETHING);
when(replicationAction.getType()).thenReturn(ReplicationActionType.DEACTIVATE);
authorListener.update(propertyMap);
authorListener.handleEvent(event);
verify(jobManager, times(1)).addJob(anyString(), anyMap());
}
}
If this does not help, please let us know your specific scenario.