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
Solved! Go to Solution.
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
try adapting the ctx.currentResource() instead of request. That should solve it if I am not wrong.
I tried doing it by ctx.currentResource() but still getting null value
eg :
locationsModel=ctx.currentResource().adaptTo(LocationsModel.class);
you have to mock
GoogleMapAPIConfiguration
how can I do that
could you please tell me please
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
@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);
@rahul234dabas I am getting same error. Could you please help me, how did you solve this issue?
Views
Like
Replies