You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AEM wcm.io测试MockProperty名称编码异常:冒号被转义为%3A致测试失败

Why Your MockProperty Tests Fail with Colon Encoding (and Fixes)

Hey there! Let's break down why your test environment is turning colons (:) into %3A while production runs smoothly, plus actionable fixes to resolve this mismatch.

Root Cause

The core issue boils down to how mock implementations (like those used in your tests—e.g., AEM Mocks or custom Mockito mocks) handle property names compared to the real JCR/Resource resolver in production.

In production, JCR natively supports colons in property names (commonly used for namespace prefixes like jcr:title or custom namespace identifiers). But many mock libraries either:

  • Automatically URL-encode special characters like colons when generating mock properties, or
  • Have a behavior mismatch/bug that doesn't replicate the real JCR's handling of colons.

Your production code works because it interacts with a genuine JCR repository that preserves raw property names—no unwanted encoding occurs there.

Fixes to Try

1. Upgrade Your Mock Library

If you're using a library like AEM Mocks (io.wcm.testing.aem-mock), check for newer versions. Mock libraries frequently patch these kinds of test/production behavior mismatches, so upgrading might resolve the encoding issue out of the box.

2. Explicitly Stub Mock Properties with Raw Names

Instead of relying on auto-generated mock properties, manually stub the property name to return the unencoded string. For example:

// Create a mock Property with the correct unencoded name
Property mockCustomProperty = mock(Property.class);
when(mockCustomProperty.getName()).thenReturn("my:customProperty");
when(mockCustomProperty.getValue()).thenReturn(ValueFactoryImpl.getInstance().createValue("test content"));

// Stub the PropertyIterator to return your mock property
PropertyIterator mockIterator = mock(PropertyIterator.class);
when(mockIterator.hasNext()).thenReturn(true, false);
when(mockIterator.nextProperty()).thenReturn(mockCustomProperty);

// Attach the iterator to your mock Node
Node mockNode = mock(Node.class);
when(mockNode.getProperties()).thenReturn(mockIterator);

This ensures the property name returned during testing matches exactly what you'd get in production.

3. Customize Mock Property Behavior

If your mock library's default Property implementation keeps encoding names, use a spy to override the getName() method:

// Spy on the existing mock to force unencoded name return
Property spyProperty = spy(yourAutoGeneratedMockProperty);
when(spyProperty.getName()).thenReturn("original:propertyName");

This bypasses the mock's default encoding logic and returns the raw colon.

4. Check for Accidental Encoding in Test Setup

Double-check your test code for unintended encoding steps. If you're using URL-encoding utilities to build resource paths or property names in your test setup, that could be the source of the %3A issue. Remove any unnecessary encoding from your test preparation code.


内容的提问来源于stack exchange,提问作者Jdruwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:39:57