0

I have following code to test:

  static Set<String> methodToTest(Node crxProductNode, OfflineNodeAction nodeAction, Set<String> allowedAttributes) throws RepositoryException {
        Set<String> changedPropertiesKeys = new HashSet<String>();
        final PropertyIterator crxProductNodeProperties = crxProductNode.getProperties();
        while (crxProductNodeProperties.hasNext()) {
            final String crxNodePropertyName = crxProductNodeProperties.nextProperty().getName();
            if(nodeAction.getProperties(crxNodePropertyName)==null && allowedAttributes.contains(crxNodePropertyName)){
                changedPropertiesKeys.add(crxNodePropertyName);
            }
        }
        return changedPropertiesKeys;
    }

Please advise me how to test following code.

I have problem with Iterator mocking. I don't understand how to mock it.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

0

Because the PropertyIterator is received from crxProductNode and it is a parameter passed to the method, it is a book definition of so called "test collaborator".

We should mock out test collaborators, in your case mock the crxProductNode to return another mock of type PropertyIterator and pass it to the method.

The concrete code depends on your mocking framework, but should be something like the following pseudo code:

@Test
public void testMyPropertyIterator() {
    Sut sut = new SUT(); // System Under Test, the class with your method

    Node nodeMock                   = mock(Node.class);
    PropertyIterator mockProperties = mock(PropertyIterator.class);

    when(nodeMock.getProperties()).thenReturn(mockProperties);

    sut.methodToTest(nodeMock, ...);

   // verification or assertions
}
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
0

For this exists class

org.apache.sling.commons.testing.jcr.MockNode

and

MockNodeIterator
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710