此问题在此处已有答案:
What is a NullPointerException, and how do I fix it?(12个答案)
三个月前关门了。
我尝试使用AemContext将JSON资源文件适配为Model,我使用@Postconstruct注解初始化模型函数,但每次运行项目时,调用getList时出现空指针异常()函数。我试图掌握单元测试这个通过@Postconstruct注入运行的模型。如果我能知道得到空指针异常的原因,这将非常有帮助。
模型接口
package com.xyz.core.models;
import java.util.List;
import java.util.Map;
public interface TableModel {
/**
* Function to be implemented to fetch
* list of table items from JCR
*
* @return list of items
*/
public List<Map<String, String>> getList();
}
模型类别
package com.xyz.core.models.impl;
import com.xyz.core.models.TableModel;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Model(adaptables = Resource.class, adapters = TableModel.class)
public class TableModelImpl implements TableModel{
@Inject
@Optional
private Resource items;
private List<Map<String, String>> itemsChildren = new ArrayList<Map<String, String>>();
@PostConstruct
public void init(){
if (items != null) {
for (Resource resource: items.getChildren()) {
ValueMap properties = resource.adaptTo(ValueMap.class);
String title = properties.get("title", String.class);
String content = properties.get("content", String.class);
Map<String, String> map = new HashMap<String, String>();
map.put("title", title);
map.put("content", content);
itemsChildren.add(map);
}
}
}
public List<Map<String, String>> getList() {
return Collections.unmodifiableList(itemsChildren);
}
}
模型类的测试类
package com.xyz.core.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import com.xyz.core.models.impl.TableModelImpl;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
public class TableModelImplTest {
private final AemContext aemContext = new AemContext();
TableModelImpl tableModelImpl;
private List<Map<String, String>> expectedItemsChildren = new ArrayList<Map<String, String>>();
private final Map<String, String> map = new HashMap<String, String>();
@BeforeEach
void setUp() {
aemContext.addModelsForClasses(TableModelImpl.class);
aemContext.load().json("/com/xyz/core/models/impl/Table.json", "/component");
}
@Test
void getList() {
aemContext.currentResource("/component/table");
tableModelImpl = aemContext.request().adaptTo(TableModelImpl.class)
map.put("title", "TItle");
map.put("content","content");
this.expectedItemsChildren.add(map);
map.put("title", "Many");
map.put("content","More");
List<Map<String, String>> actualItemsChildren = tableModelImpl.getList();
assertEquals(expectedItemsChildren, actualItemsChildren);
}
}
来自资源的JSON文件
{
"table":{
"jcr:primaryType":"nt:unstructured",
"jcr:createdBy":"admin",
"jcr:lastModifiedBy":"admin",
"jcr:created":"Wed Aug 24 2022 10:38:41 GMT+0900",
"jcr:lastModified":"Wed Aug 24 2022 10:39:03 GMT+0900",
"sling:resourceType":"xyz/components/table",
"items":{
"jcr:primaryType":"nt:unstructured",
"item0":{
"jcr:primaryType":"nt:unstructured",
"title":"TItle",
"content":"Content"
},
"item1":{
"jcr:primaryType":"nt:unstructured",
"title":"Many",
"content":"More"
}
}
}
}
1条答案
按热度按时间holgip5t1#
您的行
会尝试使吊索请求适合您的模型。
但是,您的模型声明它只能从
Resource
(adaptables = Resource.class
)开始适应。尝试
得双曲余切值.