【问题标题】:AEM Mocks: Cannot Inject ConfigAEM Mocks:无法注入配置
【发布时间】:2020-01-10 14:31:04
【问题描述】:

我正在使用AEM Mocks 来测试一个使用配置的自定义 servlet,例如:

@Activate
void activate(final Config config) { ... }

我正在按照herehere 描述的方法将服务与 HashMap 一起注册和注入,如下所示:

private static Map<String, Object> myHashMap = new HashMap<>();

...

myHashMap.put("a", "b");
myHashMap.put("c", "d");

...

servlet = context.registerInjectActivateService(new MyServlet(), myHashMap);

但是,这种方法行不通。上面传递的配置对象,在 activate 函数内,已损坏。对于上面的每个键值对,它将null 设置为值。所以而不是:

a -> b
c -> d

设置:

a -> null
c -> null

在 HashMap 内部。有人可以帮忙吗?谢谢!

附:我应该补充一点,我正在使用 2.3.0 版的 AEM Mocks,因为最近的版本会导致较旧的工件出现问题。有关这方面的更多信息,请参阅here

【问题讨论】:

    标签: unit-testing aem sling


    【解决方案1】:

    我对其进行了测试,它也适用于 2.3.0 版本。你能检查下面的例子吗?在那之后,这可能是一个maven问题。那么我们需要更多信息。

    这是我的测试 servlet:

    @Component(service = Servlet.class,
            property = {
                    SLING_SERVLET_PATHS + "=/bin/servlet/test",
                    SLING_SERVLET_METHODS + "=GET",
                    SLING_SERVLET_EXTENSIONS + "=text"
            })
    @Designate(ocd = TestServlet.Config.class)
    public class TestServlet extends SlingSafeMethodsServlet {
    
        @ObjectClassDefinition
        public @interface Config {
    
            @AttributeDefinition(
                    name = "Name",
                    description = "Name used in the hello world text"
            )
            String name() default "Alex";
    
            @AttributeDefinition(
                    name = "Greeting",
                    description = "Greeting - Morning, to demonstrate the dot-replacement"
            )
            String greeting_morning() default "Good Morning";
        }
    
        private Config config;
    
        @Override
        protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/plain");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(this.getGreeting());
        }
    
        public String getGreeting() {
            return config.greeting_morning() + ", " + config.name();
        }
    
        @Activate
        void activate(final Config config) {
            this.config = config;
        }
    }
    

    这是一个 JUnit 4 测试:

    public class TestServletTest {
    
        @Rule
        public final AemContext context = new AemContext();
    
        @Test
        public void testWithoutConfig() {
            final TestServlet testServlet = context.registerInjectActivateService(new TestServlet());
            assertEquals("Good Morning, Alex", testServlet.getGreeting());
        }
    
        @Test
        public void testWithConfig() {
            final Map<String, Object> properties = new HashMap<>();
            properties.put("name", "Berndt");
            properties.put("greeting.morning", "Keep sleeping");
            final TestServlet testServlet = context.registerInjectActivateService(new TestServlet(), properties);
    
            assertEquals("Keep sleeping, Berndt", testServlet.getGreeting());
        }
    }
    

    【讨论】:

    • 感谢您的回答。我无法最终重现您的结果,因此它确实可能是一个专家问题。我会进一步研究它 - 知道默认的做事方式确实有效并且在 2.3.0 版本中确实有效。
    • 请将您的有效 pom.xml 添加到问题中。也许我们可以调查一下(将 help:effective-pom 添加到您的 maven 命令行)
    • 我不能添加它,因为它的专有代码,但是关于私有的提示很有趣。我可能已将 AemContext 或配置设置为私有。我会调查的。谢谢!
    • 没有。我的意思是你的 pom.xml 中的服务器名称、电话号码或密码等私人内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多