Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM - junit test case for Jcr Event Listener

Avatar

Level 1

Hi,

Is there any guide for junit test cases for Jcr Event Listener for sample code as below:

https://forums.adobe.com/community/experience-cloud/marketing-cloud/experience-manager/questions 

 

Thanks,

Sundari.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @sundarig3746447 ,

Although the link that you have provided doesn't show source code but check this blog for a few examples

http://useof.org/java-open-source/javax.jcr.observation.EventIterator

 

Find another example below:

@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.

View solution in original post

3 Replies

Avatar

Level 1

Hi @vanegi,

Thanks for your reply!!

I could see only the source code in the provided link, not the test cases for the same.

Avatar

Correct answer by
Community Advisor

Hi @sundarig3746447 ,

Although the link that you have provided doesn't show source code but check this blog for a few examples

http://useof.org/java-open-source/javax.jcr.observation.EventIterator

 

Find another example below:

@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.