aem junit java.lang.nullpointerexception

发布于 2025-02-12 01:45:56 字数 1446 浏览 2 评论 0原文

我需要为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2025-02-19 01:45:56

看起来您的模型是null参考。您确实尝试使用MockItoextension模拟它,但这在很大程度上是多余的,因为您还使用了aemcontextensentions,这可能是该问题的原因。

除空指针外,此代码甚至没有测试任何内容。一切都是模拟的,甚至是test类,我理解是正在测试的主题。

另外,您要传递到 addmmodelsforclasses 看起来好像看起来像测试类(testimpl),而不是吊索模型test的类。

与其依靠摩根托,让AEM模拟库本身设置了所有基础对象,并确保您要测试的类是真实的东西,而不是模拟。

@ExtendWith(AemContextExtension.class)
class TestImpl {
    private final AemContext ctx = new AemContext();

    @BeforeEach
    void setUp() throws Exception {
        ctx.addModelsForClasses(Test.class); // Give it the Sling Model

        ctx.load().json("/com/project/core/models/adobe/TestImplTest.json","/content");
    }

    @Test
    void testGetText() {
        final String expected = "textTEST";
        ctx.currentResource("/content/text");
        Test test = ctx.request().adaptTo(Test.class); // It'll use the actual class, not a mock this way
        String actual = test.getText();
        assertEquals(expected,actual);
    }
}

参见

It looks like your model is a null reference. You do try to mock it with MockitoExtension but that's largely superfluous, given that you're also using AemContextExtension 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 Model Test.

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.

@ExtendWith(AemContextExtension.class)
class TestImpl {
    private final AemContext ctx = new AemContext();

    @BeforeEach
    void setUp() throws Exception {
        ctx.addModelsForClasses(Test.class); // Give it the Sling Model

        ctx.load().json("/com/project/core/models/adobe/TestImplTest.json","/content");
    }

    @Test
    void testGetText() {
        final String expected = "textTEST";
        ctx.currentResource("/content/text");
        Test test = ctx.request().adaptTo(Test.class); // It'll use the actual class, not a mock this way
        String actual = test.getText();
        assertEquals(expected,actual);
    }
}

See

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文