Hi,
I have created a service to create packages. I want to know how to write Junit test for this service. PFB the service code for creating package.
Any help would be great. Thanks in advance!!
@reference
private Packaging packaging;
public void createPackage(Node rootNode, String pkgName) {
JcrPackageManager jcrPackageManager = packaging.getPackageManager(session);
JcrPackage jcrPackage = jcrPackageManager.create(rootNode, pkgName);
JcrPackageDefinition jcrPackageDefinition = jcrPackage.getDefinition();
DefaultWorkspaceFilter workspaceFilter = new DefaultWorkspaceFilter();
for (PathFilterSet packageFilter : packageFilters) {
workspaceFilter.add(packageFilter);
}
jcrPackageDefinition.setFilter(workspaceFilter, AUTO_SAVE);
jcrPackageDefinition.set(JcrPackageDefinition.PN_DESCRIPTION, PACKAGE_DESCRIPTION + pkgName, false);
jcrPackageDefinition.set(JcrPackageDefinition.PN_GROUP, rootNode.getName() , false);
jcrPackageManager.assemble(jcrPackage, listener);
}
Solved! Go to Solution.
Views
Replies
Total Likes
The testing libraries I would use here are:
If you are looking for examples, take a look at this example of how they mocked the Packaging dependency, https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad....
Another example here, https://www.programcreek.com/java-api-examples/?code=Adobe-Consulting-Services/acs-aem-commons/acs-a....
I hope these examples will steer you in a better direction.
Hi @santhoshsrg
Verbally speaking, you have to mock packaging service and then use it to test every logic you have written. @Jörg_Hoh has written some articles on this topic. Please refer to these
https://cqdump.wordpress.com/2019/01/09/writing-unit-tests-for-aem-using-slingmocks/
Part 1 - https://cqdump.wordpress.com/2019/01/09/writing-unit-tests-for-aem-using-slingmocks/
Part 2- https://cqdump.wordpress.com/2019/01/14/writing-unit-tests-for-aem-part-2-maven-setup/
Part 3- https://cqdump.wordpress.com/2019/01/17/writing-unittests-for-aem-part-3-mocking-resources/
The testing libraries I would use here are:
If you are looking for examples, take a look at this example of how they mocked the Packaging dependency, https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad....
Another example here, https://www.programcreek.com/java-api-examples/?code=Adobe-Consulting-Services/acs-aem-commons/acs-a....
I hope these examples will steer you in a better direction.
Thanks @BrianKasingli and @VeenaVikraman for answers.
@BrianKasingli The first example you shared matches most of my scenario. But still I am not able to create package using that approach. I have updated my code based on that. But I am getting different exception now. PFB part of my code.
Could you please let me know if there is any mistake. The service actually works fine. The problem is happening only in Junit.
Exception:
javax.jcr.RepositoryException: Item.save() not allowed on new item
at org.apache.jackrabbit.oak.jcr.session.ItemImpl.save(ItemImpl.java:267)
at org.apache.jackrabbit.oak.jcr.session.NodeImpl.save(NodeImpl.java:105)
at org.apache.jackrabbit.vault.packaging.impl.JcrPackageImpl.createNew(JcrPackageImpl.java:221)
at org.apache.jackrabbit.vault.packaging.impl.JcrPackageManagerImpl.create(JcrPackageManagerImpl.java:344)
Junit Code:
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class CreateComplianceFileProcessTest {
public final AemContext context = new AemContext(ResourceResolverType.JCR_OAK);
@InjectMocks
private CreateComplianceFileProcess createCompFileWP;
@Test
void testExecute() {
resolver = context.resourceResolver();
session = resolver.adaptTo(Session.class);
when(complianceResolverService.getResourceResolver()).thenReturn(resolver);
when(packaging.getPackageManager(any(Session.class))).thenReturn(new JcrPackageManagerImpl(session));
createCompFileWP.execute(item, null, null);
}
}
Views
Replies
Total Likes
Views
Replies
Total Likes
In the example https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad..., they have used jcrPackageManager.create(groupName, name, version) which is also working for me. But methods mentioned below are not working. These are throwing exceptions in "org.apache.jackrabbit" classes.
Methods not working:
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies