request.AdapTo() giving null values while doing Unit testing for sling models | Community
Skip to main content
New Participant
April 19, 2022
Solved

request.AdapTo() giving null values while doing Unit testing for sling models

  • April 19, 2022
  • 3 replies
  • 3208 views

hi I have the following model class :


@Model(
adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LocationsModel extends ComponentModel implements Validatable {
private static final Logger log = LoggerFactory.getLogger(LocationsModel.class);
private static final String CSS_CLASS_NAME = "locations";

@OSGiService
private GoogleMapAPIConfiguration mapAPIConfiguration;

@ValueMapValue
private String title;

@ValueMapValue
private int zoomLevel;

@ChildResourceFromRequest
private final List<LocationItem> locations = emptyList();

private String locationsJson;
private String key;

public LocationsModel() {
}

@PostConstruct
protected void init() {
if (mapAPIConfiguration != null) {
this.key = mapAPIConfiguration.getKey();
}

try {
this.locationsJson = new ObjectMapper().writeValueAsString(this.locations);
} catch (JsonProcessingException ex) {
log.error("Unable to generate locations to JSON string", ex);
}
}

@Nonnull
@Override
public String getCssClassName() {
return CSS_CLASS_NAME;
}

@Override
public boolean isValid() {
return StringUtils.isNotEmpty(this.key) && !this.locations.isEmpty();
}

public String getTitle() {
return this.title;
}

@Nonnull
public List<LocationItem> getLocations() {
return this.locations;
}

public String getLocationsJson() {
return this.locationsJson;
}

public int getZoomLevel() {
return this.zoomLevel;
}

public String getKey() {
return this.key;
}
}

and the test class is :

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class LocationsModelTest {

Private final AemContext ctx = new AemContext();
public LocationsModel locationsModel;
@BeforeEach
void setUp() throws Exception {
ctx.addModelsForClasses(LocationsModel.class);
ctx.load().json("/locations/LocationsModelTest.json","/content");
}
@Test
void getZoomLevel() {
ctx.currentResource("/content/location");
System.out.println("ctx "+ctx.request().adaptTo(LocationsModel.class));
locationsModel=ctx.request().adaptTo(LocationsModel.class); // GETTING NULL VALUE HERE

int expected = 10;
int actual = locationsModel.getZoomLevel();
locationsModel.getLocations();
assertEquals(expected,actual);
}
}

I am not able to find out why this line is giving null value ..

the paths are correct and I have provided the JSON also in the same path as mentioned but somehow getting null  in " locationsModel=ctx.request().adaptTo(LocationsModel.class);" 

please help me resolve the issue

Thank you

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by raushan123

you have to mock the service and after that you have to register the service 

ex:-

@Mock
MyAccountService myAccountService;
@BeforeEach
public void setUp() throws Exception {
ctx.registerService(MyAccountService.class, myAccountService);
ctx.addModelsForClasses(test.class);
testModel=ctx.request().adaptTo(test.class);

follow this document.

https://wcm.io/testing/aem-mock/usage.html

 

3 replies

New Participant
April 25, 2022

@ChildResourceFromRequest is creating issue here @raushan123 @b_sravan 

is there any alternative for it?

if I remove the annotation then this line works fine 

locationsModel=ctx.request().adaptTo(LocationsModel.class);
New Participant
October 4, 2022

@rahul234dabas I am getting same error. Could you please help me, how did you solve this issue?

New Participant
April 20, 2022

you have to mock 

GoogleMapAPIConfiguration
New Participant
April 20, 2022

how can I do that

could you please tell me please

raushan123Accepted solution
New Participant
April 20, 2022

you have to mock the service and after that you have to register the service 

ex:-

@Mock
MyAccountService myAccountService;
@BeforeEach
public void setUp() throws Exception {
ctx.registerService(MyAccountService.class, myAccountService);
ctx.addModelsForClasses(test.class);
testModel=ctx.request().adaptTo(test.class);

follow this document.

https://wcm.io/testing/aem-mock/usage.html

 

B_Sravan
New Participant
April 19, 2022

try adapting the ctx.currentResource() instead of request. That should solve it if I am not wrong.

New Participant
April 20, 2022

I tried doing it by ctx.currentResource() but still getting null value

eg : 

locationsModel=ctx.currentResource().adaptTo(LocationsModel.class);