如何使 SlingHttpServletRequest.getParts() 在 JUnit Test 中返回正确的值?



我在编写junit测试以通过slingHttpServletRequest.getParts((的getParts((方法的for循环条件时遇到了一些困难。实现没有问题,我能够正确处理文件附件。但是,我无法在 junit 测试中这样做。

以下是我的实现:

@Model(adaptables = SlingHttpServletRequest.class)
public class Comment {
//Variables declaration
@Inject
private CommentService service;
@PostConstruct
public void setup() {
requestData = new JSONObject();

for (String item : request.getRequestParameterMap().keySet()) {
try {
requestData.put(item, request.getParameter(item));
}
} catch (Exception e) {
Throw error message
}
}
//Upload attachment to server
try {
for (Part part : request.getParts()) {      <= The JUnit test stopped at this line and throw the error below
} catch (Exception e) {
Throw error message
}

我尝试使用SlingHttpServletRequestWrapper类来覆盖getParts方法,但无济于事。

以下是我的 junit 测试:

public class CommentTest {
public final AemContext context = new AemContext();
private CommentService commentService = mock(CommentService.class);
@InjectMocks
private Comment comment;
private static String PATH = "/content/testproject/en/page/sub-page";
@Before
public void setUp() throws Exception {
context.addModelsForPackage("de.com.adsl.sightly.model");
context.load().json("/components/textrte.json", PATH);
context.currentPage(PATH);
}
@Test
public void testSetup() throws IOException, ServletException {
//before
context.request().setParameterMap(getRequestCat1());
context.registerService(CommentService.class, commentService);
Resource resource = context.resourceResolver().getResource(PATH + "/jcr:content/root/responsivegrid/textrte");
assertNotNull(resource);
//when
comment = new CustomRequest(context.request()).adaptTo(Comment.class);
//then
comment.setup();
}
private class CustomRequest extends SlingHttpServletRequestWrapper {
public CustomRequest(SlingHttpServletRequest request) {
super(request);
}
@Override
public Collection<Part> getParts() {
final String mockContent =
"------WebKitFormBoundarycTqA2AimXQHBAJbZn" +
"Content-Disposition: form-data; name="key"n" +
"n" +
"myvalue1n" +
"------WebKitFormBoundarycTqA2AimXQHBAJbZ";
final List<Part> parts = MockPart.parseAll(mockContent);
assertNotNull(parts);
return parts;
}
};
}

以下是我遇到的错误消息:

14:53:04.918 [main] ERROR de.com.adsl.sightly.model.Comment - Error Message: null
java.lang.UnsupportedOperationException: null
at org.apache.sling.servlethelpers.MockSlingHttpServletRequest.getParts(MockSlingHttpServletRequest.java:882) ~[org.apache.sling.servlet-helpers-1.1.10.jar:?]
at de.com.adsl.sightly.model.Comment.uploadFile(Feedback.java:137) ~[classes/:?]
at de.com.adsl.sightly.model.Comment.setup(Feedback.java:82) [classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
at org.apache.sling.models.impl.ModelAdapterFactory.invokePostConstruct(ModelAdapterFactory.java:792) [org.apache.sling.models.impl-1.3.8.jar:?]
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:607) [org.apache.sling.models.impl-1.3.8.jar:?]
at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:335) [org.apache.sling.models.impl-1.3.8.jar:?]
at org.apache.sling.models.impl.ModelAdapterFactory.getAdapter(ModelAdapterFactory.java:211) [org.apache.sling.models.impl-1.3.8.jar:?]
...

我在网上查找了各种解决方案,例如在陈述时写了两个模拟,但没有成功。如果您以前遇到过以下问题,我将非常感谢任何形式的帮助或知识共享。谢谢!

MockSlingServletResquest的源代码来看,它总是抛出该异常,因为模拟类尚不支持该异常。

https://github.com/apache/sling-org-apache-sling-servlet-helpers/blob/71ef769e5564cf78e49d6679a3270ba8706ae406/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequest.java#L953

也许你应该考虑编写一个servlet,或者其他方法。

相关内容

  • 没有找到相关文章

最新更新