aem junit java.lang.nullpointerexception
我需要为AEM项目中的类创建一个JUNIT测试,并且我有NullPoInterException问题: 我创建了ClasStestimpl
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class TestImpl {
private final AemContext ctx = new AemContext();
@Mock
private Test test;
@Mock
private ModelFactory modelFactory;
@BeforeEach
void setUp() throws Exception {
ctx.addModelsForClasses(TestImpl.class);
ctx.load().json("/com/project/core/models/adobe/TestImplTest.json","/content");
lenient().when(modelFactory.getModelFromWrappedRequest(eq(ctx.request()),
any(Resource.class), eq(Test.class)))
.thenReturn(test);
}
@Test
void testGetText() {
final String expected = "textTEST";
ctx.currentResource("/content/text");
Test test = ctx.request().adaptTo(Test.class);
String actual = test.getText();
assertEquals(expected,actual);
}
和JSON结构:
"text": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "project/components/core/title",
"text": "textTEST"
}
}
当我运行测试时,我给出了结果:
@Test
void testGetText() {
final String expected = "titleTEST";
ctx.currentResource("/content/title");
Title title = ctx.request().adaptTo(Title.class);
-->String actual = title[NullPointerException].getText();<--
assertEquals(expected,actual);
}
I need to create a Junit test for a class in an AEM project and I'm having NullPointerException problems:
I create the ClassTestImpl
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class TestImpl {
private final AemContext ctx = new AemContext();
@Mock
private Test test;
@Mock
private ModelFactory modelFactory;
@BeforeEach
void setUp() throws Exception {
ctx.addModelsForClasses(TestImpl.class);
ctx.load().json("/com/project/core/models/adobe/TestImplTest.json","/content");
lenient().when(modelFactory.getModelFromWrappedRequest(eq(ctx.request()),
any(Resource.class), eq(Test.class)))
.thenReturn(test);
}
@Test
void testGetText() {
final String expected = "textTEST";
ctx.currentResource("/content/text");
Test test = ctx.request().adaptTo(Test.class);
String actual = test.getText();
assertEquals(expected,actual);
}
and the json structure:
"text": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "project/components/core/title",
"text": "textTEST"
}
}
when i Run test i give that result:
@Test
void testGetText() {
final String expected = "titleTEST";
ctx.currentResource("/content/title");
Title title = ctx.request().adaptTo(Title.class);
-->String actual = title[NullPointerException].getText();<--
assertEquals(expected,actual);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的模型是
null
参考。您确实尝试使用MockItoextension
模拟它,但这在很大程度上是多余的,因为您还使用了aemcontextensentions
,这可能是该问题的原因。除空指针外,此代码甚至没有测试任何内容。一切都是模拟的,甚至是
test
类,我理解是正在测试的主题。另外,您要传递到
addmmodelsforclasses
看起来好像看起来像测试类(testimpl
),而不是吊索模型test
的类。与其依靠摩根托,让AEM模拟库本身设置了所有基础对象,并确保您要测试的类是真实的东西,而不是模拟。
参见
It looks like your model is a
null
reference. You do try to mock it withMockitoExtension
but that's largely superfluous, given that you're also usingAemContextExtension
and it's probably the cause of the issue.Null pointers aside, this code doesn't even test anything. Everything is mocked, even the
Test
class which I understand to be the subject under test.Also, the parameter you're passing to
addModelsForClasses
looks like the test class (TestImpl
) rather than the class of the Sling ModelTest
.Instead of relying on Mockito, let the AEM Mocks library set up all the underlying objects by itself and make sure the class you're testing is the real thing, rather than a mock.
See