JUnit Help | Community
Skip to main content
May 21, 2024
Solved

JUnit Help

  • May 21, 2024
  • 2 replies
  • 677 views

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

@1497330(values = "defaultDescription")

private String descriptionView;

@Inject

@1497330(values = "")

private String dataCardVariation;

@9944223

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

 

@2785667

void testGetRowsDescription() {

 

 

List nrows= cardModel.getRowsDescription();

 

 

assertEquals(1,nrows.size());

 

}

 

}

 

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

2 replies

sravs
Community Advisor
sravsCommunity AdvisorAccepted solution
Community Advisor
May 22, 2024
HrishikeshKagne
Community Advisor
Community Advisor
May 22, 2024

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.

Hrishikesh Kagane