I have the below JUNIT code. Case 1 I get Nullpointer exception error when I tried to execute it in Eclipse and I get all test cases passed when I run it in command prompt.
Case 2 - I get Nullpointer exception error error sometime even when I ran the code in command prompt with to execute. Maven command used - mvn clean install -PautoInstallBundle
I guess it is because of the resource I am loading. But it this resource is always there in eclipse build path.
Junit code
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.techcombank.core.constants.TestConstants;
import com.techcombank.core.testcontext.AppAemContext;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import com.day.cq.wcm.api.WCMMode;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
public class FeatureListModelTest {
private final AemContext ctx = AppAemContext.newAemContext();
FeatureListModel featureModel;
@BeforeEach
void setUp() throws Exception {
ctx.load().json("/content/mysite/en/featurelist.json", "/content");
ctx.addModelsForClasses(FeatureListModel.class);
featureModel = getModel("/content/jcr:content/root/feature");
}
@Test
void getFeaturePath() {
final String expected = "/content/dam/mysite/logo.jpg";
assertEquals(expected, featureModel.getFeaturePath());
}
private FeatureListModel getModel(String currentResource) {
ctx.request().setAttribute(WCMMode.class.getName(), WCMMode.EDIT);
ctx.currentResource(currentResource);
FeatureListModel fm = ctx.currentResource().adaptTo(FeatureListModel.class);
return fm;
}
}
Any idea on case 1 and case 2. Why is this consistent happen ? In fact I restarted eclipse and my windows machine but no luck.