gpt4 book ai didi

java - 进行 Junit 测试时,如何在 AEM Sling 模型中的节点对象中 setProperty()?

转载 作者:行者123 更新时间:2023-12-01 19:32:35 24 4
gpt4 key购买 nike

我尝试在单元测试中的节点类中设置属性,但无法这样做。如果您以前碰巧这样做过,我想寻求您的帮助,分享您在 AEM Sling MOdel 中设置节点属性以进行单元测试的知识。谢谢!

实现如下:

@Model(adaptables = SlingHttpServletRequest.class)
public class SlingModelClass {

@Inject
private SomeService someService;
private boolean isShowCaptcha;

@SlingObject
private Resource resource;

private Node node;

@PostConstruct
public void setup() {
node = resource.adaptTo(Node.class);
isShowCaptcha = someService.isCaptchaHide();
hideShowTitle(!isShowCaptcha); <= Test unable to get through this method
}

public boolean getIsShowCaptcha() {
return this.isShowCaptcha;
}

public PropertyIterator getPropertiesItems() throws RepositoryException {
return this.node.getProperties();
}

private void hideShowTitle(boolean flag) {
try {
node.setProperty("hideTitle", flag); <= I am getting a null pointer exception at this line
if(flag) {
node.setProperty("guideNodeClass", "");
} else {
node.setProperty("guideNodeClass", "guideCaptcha");
}
node.getSession().save();

} catch (RepositoryException e) {
e.printStackTrace();
}
}
}

我编写的单元测试:

public class SlingModelClassTest {

@Rule
public final AemContext context = new AemContext();

@Mock
private SomeService someService = mock(SomeService .class);

@Mock
Resource resource;

// injects all the mocks into the tested object.
@InjectMocks
private SlingModelClass slingModelClass;

private Node node;

private static String PATH = "/content/testproject/en/page/sub-page";

@Before
public void setUp() {
context.addModelsForPackage("de.com.adsl.sightly.model");
context.load().json("/components/textrte.json", PATH);
context.currentPage(PATH);
resource = mock(Resource.class);
}

@Test
public void testSetup() {

context.registerService(SomeService.class, someService);

slingModelClass.setup();
}

错误如下:

java.lang.NullPointerException
at de.com.adsl.sightly.model.SlingModelClassTest.aaa(SlingModelClassTest.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

最佳答案

这是错误的模式。 Sling 模型不应改变 JCR 节点。当每次实例化 Sling 模型时,它都会在存储库上执行写入操作,您可能会遇到大麻烦。 Sling 模型应该从 JCR 读取。

如果您需要一些全局标志,请使用页面属性和继承来读取该标志并在必要时覆盖它。这样,配置就以受控的方式完成,您只需在必要时编写属性。

只是为了完成我的答案,正如 @OliverGebert 所建议的:Sling API 应该用于处理存储库数据/作为抽象层。 JCR API 被认为级别太低,而 AEM 基本上构建在 Sling 之上。此外,您还需要管理 Sling 以及 JCR 依赖项,这在长期项目中可能是一个很大的麻烦。

关于java - 进行 Junit 测试时,如何在 AEM Sling 模型中的节点对象中 setProperty()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59123158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com