Expand my Community achievements bar.

SOLVED

AEM package creation programmatically- writing Junit TC for package creation

Avatar

Level 1

Hi All,

      I was creating AEM package programattically for a usecase. while writing the test case for create package method im getting NPE, though all param was not null. Kinldy help on mocking below method for Junit.

method needs to be mocked below : 

packageHelper.createPackage(packageResource, session,PageConstants.DEFAULT_GROUP_NAME, "testPackage", PageConstants.DEFAULT_VERSION,
PackageHelper.ConflictResolution.Replace, packageDefinitionProperties);

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi, 

 

Assuming `PackageHelper` is from the ACSCommons package, you just need to mock the whole service and then mock the method with exactly the parameters you will be passing in runtime. 


Something like this:

@Mock
private PackageHelper packageHelperMock;

//Then you can mock the method createPackage
when(packageHelperMock.createPackage(.....your expected parameter)).thenReturn(yourExpectedValue);

 



Esteban Bustamante

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi, 

 

Assuming `PackageHelper` is from the ACSCommons package, you just need to mock the whole service and then mock the method with exactly the parameters you will be passing in runtime. 


Something like this:

@Mock
private PackageHelper packageHelperMock;

//Then you can mock the method createPackage
when(packageHelperMock.createPackage(.....your expected parameter)).thenReturn(yourExpectedValue);

 



Esteban Bustamante

Hi @EstebanBustamante , thanks much for your reply. you're rite... `PackageHelper` was from the ACSCommons package.as shown in the earlier screenshot attached, all param was not null. In the servlet test case file, while debug, im getting mock Jcrpackage as out come. when it reach to actual servlet packagehelper.create(your expected parameter) showing jcrpackage as Null, though all param's were not null.