AEM - junit test case for Jcr Event Listener

Avatar

Avatar
Level 1
sundarig3746447
Level 1

Likes

0 likes

Total Posts

2 posts

Correct reply

0 solutions
View profile

Avatar
Level 1
sundarig3746447
Level 1

Likes

0 likes

Total Posts

2 posts

Correct reply

0 solutions
View profile
sundarig3746447
Level 1

28-07-2020

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 250
MVP
ChitraMadan
MVP

Likes

272 likes

Total Posts

165 posts

Correct reply

67 solutions
Top badges earned
Boost 250
Establish
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Boost 250
MVP
ChitraMadan
MVP

Likes

272 likes

Total Posts

165 posts

Correct reply

67 solutions
Top badges earned
Boost 250
Establish
Ignite 1
Give Back 5
Give Back 3
View profile
ChitraMadan
MVP

28-07-2020

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.

Answers (1)

Answers (1)

Avatar

Avatar
Give Back 5
Employee
vanegi
Employee

Likes

392 likes

Total Posts

378 posts

Correct reply

148 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 10
Give Back
Boost 50
View profile

Avatar
Give Back 5
Employee
vanegi
Employee

Likes

392 likes

Total Posts

378 posts

Correct reply

148 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 10
Give Back
Boost 50
View profile
vanegi
Employee

28-07-2020