我需要为正在用于业务逻辑并与AEM交互的Java类编写JUnit测试用例。我没有找到编写JUnit测试用例的正确文档。如果您有任何此类文档,请求您与我分享。如何在AEM6.2中编写适用于Java类的Junit测试用例
-2
A
回答
0
不知道你在找什么。 AEM类的Junit测试用例与您编写的任何Java类没有什么不同。如果需要的话,你可能需要一个模拟框架来模拟Sling对象。使用的Mockito
public class DisplayAsDataSourceWcmUse extends WCMUsePojo {
/**
* Display as script regex.
*/
private static final String DISPLAY_AS_REGEX_EXPR = "^displayAs\\_(.*)\\.html$";
/**
* Datasource to the display listing.
*/
private DataSource dataSource;
/**
* activate method.
*
* @throws Exception
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
@Override
public final void activate() throws Exception {
this.buildDisplayAsOptions();
}
/**
* Build the list of views for the DisplayAs Option.
*/
private void buildDisplayAsOptions() {
final SlingHttpServletRequest request = getRequest();
final Resource componentResource = getCurrentComponent();
List<Resource> resourceList = Collections.emptyList();
if (null != componentResource) {
final Iterable<Resource> children =() -> componentResource.listChildren();
final Stream<Resource> childResourceStream = StreamSupport.stream(children.spliterator(), false);
resourceList = childResourceStream
.filter(resource -> resource
.getName().matches(DISPLAY_AS_REGEX_EXPR))
.map(this::getMappedResource)
.collect(Collectors.toList());
childResourceStream.close();
}
if (resourceList.isEmpty()) {
dataSource = EmptyDataSource.instance();
} else {
dataSource = new SimpleDataSource(resourceList.iterator());
}
// place it in request for consumption by datasource mechanism
request.setAttribute(DataSource.class.getName(), dataSource);
}
/**
* Get resource instance of current component i.e. resource with path to component under apps/.
*
* @return component resource.
*/
private Resource getCurrentComponent() {
final Resource listResource = getResourceResolver().getResource((String) getRequest().getAttribute(Value
.CONTENTPATH_ATTRIBUTE));
if (listResource != null) {
return getResourceResolver().getResource(listResource.getResourceType());
}
return null;
}
/**
* Get Mapped Synthetic resource corresponding to the Display View.
*
* @param resource - Resource instance of display script.
* @return ValueMapResource transformation of display script.
*/
@SuppressWarnings("unused")
private ValueMapResource getMappedResource(final Resource resource) {
final String value = resource.getName().replaceFirst(DISPLAY_AS_REGEX_EXPR, "$1");
// see if there's a better label
final String text = ResourceUtil.getValueMap(resource).get(JcrConstants.JCR_TITLE, value);
// create a ValueMap
final Map<String, Object> map = new HashMap();
map.put("text", text);
map.put("value", value);
// return a synthetic resource
return new ValueMapResource(getResourceResolver(), new ResourceMetadata(), "", new ValueMapDecorator(map));
}
/**
* Getter for dataSource.
*
* @return Datasource.
*/
public final DataSource getDataSource() {
return dataSource;
}
}
一种可能的方式进行单元测试 -
周围嘲笑为OSGI和吊索类的细节在Apache吊索现场here与样本代码在here
描述的样品WCMUsePojo类(不是很理想的方式,因为你最终写了很多模拟,在这种情况下集成测试是最合适的) -
@RunWith(MockitoJUnitRunner.class)
public class DisplayAsDataSourceWcmUseTest {
@Mock
private Bindings bindings;
@Mock
private Resource currentResource;
@Mock
private Resource componentResource;
@Mock
private ResourceResolver resourceResolver;
@Mock
private SlingHttpServletRequest request;
@Mock
private Iterator<Resource> childItr;
@Mock
private Resource displayAsOne;
@Mock
private Resource displayAsTwo;
@InjectMocks
private DisplayAsDataSourceWcmUse displayAsDataSourceWcmUse;
@Before
public void setup() {
when(bindings.get("resource")).thenReturn(currentResource);
when(bindings.get("request")).thenReturn(request);
when(request.getResourceResolver()).thenReturn(resourceResolver);
when(request.getAttribute(Value.CONTENTPATH_ATTRIBUTE)).thenReturn("list/component/instance");
when(currentResource.getResourceType()).thenReturn("current/resource/type");
when(resourceResolver.getResource("current/resource/type")).thenReturn(componentResource);
when(resourceResolver.getResource("list/component/instance")).thenReturn(currentResource);
List<Resource> childRes = new ArrayList<>();
childRes.add(displayAsOne);
childRes.add(displayAsTwo);
when(componentResource.listChildren()).thenReturn(childRes.iterator());
when(displayAsOne.getName()).thenReturn("displayAs_One.html");
when(displayAsTwo.getName()).thenReturn("displayAs_Two.html");
Map<String, Object> vm1 = new HashMap();
vm1.put(JcrConstants.JCR_TITLE, "Display Type One");
when(displayAsOne.getValueMap()).thenReturn(new ValueMapDecorator(vm1));
when(displayAsTwo.getValueMap()).thenReturn(new ValueMapDecorator(new HashMap()));
}
@Test
public void test_display_option_are_set() throws Exception{
displayAsDataSourceWcmUse = new DisplayAsDataSourceWcmUse();
displayAsDataSourceWcmUse.init(bindings);
assertTrue(displayAsDataSourceWcmUse.getDataSource() instanceof SimpleDataSource);
}
@Test
public void test_empty_datasource_is_set() throws Exception{
List<Resource> childRes = new ArrayList<>();
when(componentResource.listChildren()).thenReturn(childRes.iterator());
displayAsDataSourceWcmUse = new DisplayAsDataSourceWcmUse();
displayAsDataSourceWcmUse.init(bindings);
assertTrue(displayAsDataSourceWcmUse.getDataSource() instanceof EmptyDataSource);
}
}
0
看看 'ContiPerf 2'。
ContiPerf是一个轻量级测试实用程序,使用户能够轻松利用JUnit 4测试用例作为性能测试,例如,用于持续的性能测试。它是由JUnit 4中与注释简单的测试配置和包装性能测试单元测试的JUnitPerf的想法的启发,但功能更强大,使用更简单:
- 使用Java注解,可以定义测试执行的特性研究和性能要求。
- 您可以标记测试运行一定次数或在一定时间内重复执行测试。
- 性能要求可以是最大,平均,中等或任何百分位执行时间。
- 性能要求的完美控制和可读性您可以在两种不同的模式下运行测试,将它们用作简单的单元测试或性能测试。
- 与Eclipse和Maven轻松集成。
- 将执行摘要导出到CSV文件。
- 没有外部依赖项的小型库(仅限JUnit)。
- 易于扩展与自定义统计评估和跟踪机制。
相关问题
- 1. 如何编写低于类的junit测试用例
- 2. 如何编写junit mokito测试用例
- 3. 如何在Spring Mapper类中编写junit测试用例
- 4. 如何为下面的类编写JUnit测试用例?
- 5. 编写DAO的Junit测试用例
- 6. 如何编写JUnit测试?
- 7. 关于JUnit测试用例
- 8. 单元:如何使用jUnit和Mockito编写测试用例
- 9. 如何编写未知输出的JUnit测试用例
- 10. 如何编写JUnit测试用例来检查值的范围?
- 11. 如何编写用于测试“30天过期”之类的测试用例?
- 12. 有例外的Java JUnit测试用例
- 13. 关于java类的junit测试用例的生成
- 14. Maven JUnit测试用例测试类NoClassDefFoundException
- 15. 如何编写JUnit类来测试一些在tomcat中运行的java类?
- 16. 如何编写一个junit测试用例添加方法?
- 17. 如何编写JUNIT测试用例进行嘲讽?
- 18. 如何为数据库组件编写jUnit测试用例?
- 19. 如何编写用于测试webservice的Junit
- 20. 编写JUnit测试
- 21. 如何编写测试用例Clojure中
- 22. 使用单例类在java中的junit测试用例中调用方法
- 23. 编写JUnit测试案例对Struts
- 24. 如何将我的测试类转换为JUnit测试用例?
- 25. 如何在Java中为剪贴板编写JUnit测试?
- 26. 如何编写用于OSGi(集成)测试的Arquillian测试类?
- 27. 如何在Junit测试用例中测试JcrPackageManager中的汇编API
- 28. 如何编写Controller的测试用例?
- 29. 如何编写$ q.all的测试用例
- 30. 适用于Mac的测试案例编写软件?
检查此链接,对初学者非常有用:http://www.wemblog.com/2016/12/how-to-write-tests-in-aem.html –