我在编写junit测试以将SlingHttpServletRequest.getParts()的for循环条件传递给getParts()方法时遇到一些困难。实现没有问题,我能够正确处理文件附件。但是,我无法在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 =
                    "------WebKitFormBoundarycTqA2AimXQHBAJbZ\n" +
                            "Content-Disposition: form-data; name=\"key\"\n" +
                            "\n" +
                            "myvalue1\n" +
                            "------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或其他方法。

07-24 21:46