Expand my Community achievements bar.

SOLVED

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

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Level 4

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

 

View solution in original post

7 Replies

Avatar

Community Advisor

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

Avatar

Level 5

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

eg : 

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

Avatar

Level 4

you have to mock 

GoogleMapAPIConfiguration

Avatar

Correct answer by
Level 4

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

 

Avatar

Level 5

@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);

Avatar

Level 2

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