Hi Team,
Error:
java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "this.descriptionView" is null
at com.aem.xxx.core.models.impl.DataCardModelImpl.getRowsDescription(DataCardModelImpl.java:33)
Below are interface, model class and test class respectively. It does not work. May you plese help us out
INTERFACE:
import java.util.List;
public interface DataCardModel {
public List getRowsDescription();
}
Sling MODEL:
@Model(adaptables = {Resource.class},
adapters = DataCardModel.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DataCardModelImpl implements DataCardModel {
@Inject
private int nRowsDescription;
@Inject
@default(values = "defaultDescription")
private String descriptionView;
@Inject
@default(values = "")
private String dataCardVariation;
public List getRowsDescription() {
List nrows = new ArrayList<>();
if (descriptionView.equals("noIconDescription") && dataCardVariation.equals("campodescrizione")) {
nRowsDescription = 1;
}
for (int i = 0; i < nRowsDescription; i++) {
nrows.add(i);
}
return nrows;
}
}
NOW TEST CLASS:
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class DataCardModelImplTest {
String descriptionView="noIconDescription";
int nRowsDescription;
String dataCardVariation="campodescrizione";
DataCardModel cardModel =new DataCardModelImpl();
void testGetRowsDescription() {
List nrows= cardModel.getRowsDescription();
assertEquals(1,nrows.size());
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
@ktnr , please refer below document for writing Junits for slingmodels
https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/
@ktnr , please refer below document for writing Junits for slingmodels
https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/
Hi @ktnr ,
The `NullPointerException` is occurring because the `descriptionView` field is not being injected properly in the `DataCardModelImpl` class.
To fix this issue, you can modify the test class to set the value of the `descriptionView` field before calling the `getRowsDescription()` method. Here's an example of how you can modify the test class:
```
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class DataCardModelImplTest {
int nRowsDescription;
String dataCardVariation = "campodescrizione";
DataCardModel cardModel = new DataCardModelImpl();
@Test
void testGetRowsDescription() {
// set the value of the descriptionView field
((DataCardModelImpl) cardModel).descriptionView = "noIconDescription";
List<Integer> nrows = cardModel.getRowsDescription();
assertEquals(1, nrows.size());
}
}
```
In this modified test class, the `descriptionView` field is set to `"noIconDescription"` before calling the `getRowsDescription()` method. This ensures that the `descriptionView` field is not null and the `NullPointerException` is avoided.
Note that this is just an example and you may need to modify the code to fit your specific use case. It's also recommended to consult with experienced AEM developers or solution architects to ensure that the code is implemented correctly and follows best practices.
Views
Likes
Replies