CQ-编写服务器端JUnit测试 [英] CQ - Writing Server-side JUnit tests

查看:112
本文介绍了CQ-编写服务器端JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我的一个Java类编写一个JUnit测试用例,该用例创建了一个在CQ中具有某些给定属性的页面.为此,它需要获取SlingRepository和ResourceResolverFactory的引用.我正在使用 ,以获取有关如何实现此目标的想法.在该文档中,它说到"http://$ HOST:$ PORT/system/sling/junit/"路径的POST用于在服务器端执行测试.但是在CQ中,此路径出现404错误.

I've been trying to write a JUnit test case for one of my Java class which creates a page with some given properties in CQ. For it, it need to get reference of SlingRepository and ResourceResolverFactory. I was using this to get an idea on how to achieve this. In the document it says that a POST to "http://$HOST:$PORT/system/sling/junit/" path is used to execute tests on server side. But in CQ I get a 404 error for this path.

CQ中是否有其他替代网址?或者,如果有人可以提出更好的方法,我们将不胜感激?

Is there any alternative URL in CQ for this? Or will really appreciate if anyone can suggest a better approach?

谢谢

推荐答案

一种方法是使用Sling测试运行器通过浏览器执行JUnit测试.这就是您要提到的方法.我们必须首先在此JAR中安装代码( org .apache.sling.junit.core )添加允许您列出的网址正常工作的代码.有了该代码后,该URL将允许您使用测试运行器的内置页面来运行测试,以运行/显示测试:http://localhost:4502/system/sling/junit/).我的团队做了一段时间,但是我们很快转向了另一种方法-使用Intellij IDE开发用于CQ的Java代码并编写JUnit测试,然后使用内置的JUnit测试运行程序在IDE中执行它们. Eclipse中也可以使用相同的方法.对于我们的团队而言,这种方法更为出色,因为它允许开发人员将其保留在IDE中的上下文中,而无需切换到浏览器来运行测试.

One approach is to use a Sling test runner to execute the JUnit tests via a browser. This is the approach you are mentioning. We had to first install the code in this JAR (org.apache.sling.junit.core) to add the code that allows the URL you listed to work. Once that code is there, this URL will allow you to run tests using the test runner's built in page to run/display tests: http://localhost:4502/system/sling/junit/). My team did this for a while, but we soon moved to a different approach--using the Intellij IDE to develop the Java code for CQ and write the JUnit tests, then executing them within the IDE using the built-in JUnit test runner. The same approach works in Eclipse. For our team this approach was superior because it allowed developers to remain in context in the IDE without having to switch to a browser to run the tests.

关键是能够解析对通过CQ安装/可用的类的引用,例如SlingRepository和ResourceResolverFactory类,以及我们常用的其他内容,例如Resource,ResourceResolver,Node和Session类.我们使用CQ扩展名( http://helpx.adobe.com/experience-manager /kb/HowToUseCQ5AsMavenRepository.html ),以允许我们的CQ实例充当Maven存储库.这使我们能够导出CQ JAR,以便在需要使用CQ本身可用的某些类时,可以将它们作为所创建的Java项目中的依赖项进行引用.

The key is being able to resolve the references to classes that are installed/available via CQ, such as the SlingRepository and ResourceResolverFactory classes--and other stuff we commonly used, such as the Resource, ResourceResolver, Node, and Session classes. We use a CQ extension (http://helpx.adobe.com/experience-manager/kb/HowToUseCQ5AsMavenRepository.html) to allow our CQ instance to act like a Maven repository. This allows us to export the CQ JARs so we can then reference them as dependencies in the Java projects we create whenever we may need to use some of the classes available via CQ itself.

一旦我们设置了项目依赖项,就可以在Intellij IDE中编写代码以及相应的单元测试.我们能够在IDE中运行测试,从而使开发人员可以留在上下文中并处理将在CQ中运行的代码,就像在任何Java代码上工作一样(包括以调试模式运行或在代码覆盖范围内运行测试等操作)单个测试,运行一个类中的所有测试,使用键盘快捷键启动测试等).对我们来说,这种方法比基于浏览器的Sling测试运行程序具有很多优势,所以我推荐这种方法.

Once we set up the project dependencies, then we were able to write code--and corresponding unit tests--within the Intellij IDE. We were able to run the tests within the IDE, allowing developers to remain in context and work on the code that will run in CQ just like they work on any Java code (including things like running tests in debug mode or with code coverage, running single tests, running all tests in a class, using keyboard shortcuts to kick off tests, etc.). For us this approach had many advantages over the browser-based Sling test runner, so I recommend this approach.

一些潜在的注意事项:

  • 从CQ导出为Maven存储库可能不是最好的性能-您可能希望将内容添加到自己的Maven存储库中以加快访问速度
  • 您可能需要编写一些步骤的脚本,因此添加项目依赖项不是手动过程,而是通过自动化过程完成的事情
  • 您甚至可以导出所有个CQ JAR,或者添加一些脚本以仅解析和重新打包公共类,并使任何CQ类可用于您的Java项目
  • Exporting from CQ as a Maven repo may not be the best performance--you may want to add things to your own Maven repo for faster access
  • You may want to script some of the steps so adding project dependencies is not a manual process, but rather is something done via an automated process
  • You could even export all CQ JARs--or add some scripting to parse out and repackage only the public classes--and make any CQ class available to your Java projects

这篇关于CQ-编写服务器端JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆