Munit 4:有没有办法在套件的不同测试用例中模拟来自相同缓存范围的 HTTP 请求的有效负载?

Munit 4: Is there a way to mock a payload from the same cache-scoped HTTP Request in different test cases for a suite?

我正在尝试模拟来自 HTTP 请求的响应负载,如下所示:

        <munit-tools:mock-when doc:name="doc-name"
            doc:id="doc-id" processor="http:request">
            <munit-tools:with-attributes>
                <munit-tools:with-attribute
                    attributeName="doc:name" whereValue="#['DOCNAME']" />
            </munit-tools:with-attributes>
            <munit-tools:then-return>
                <munit-tools:payload mediaType="application/json"
                    value="#[MunitTools::getResourceAsString('testPayloadA.json')]" />
            </munit-tools:then-return>
        </munit-tools:mock-when>

然后在另一个测试用例中,我像这样模拟相同的 HTTP 请求:

        <munit-tools:mock-when doc:name="doc-name"
            doc:id="doc-id" processor="http:request">
            <munit-tools:with-attributes>
                <munit-tools:with-attribute
                    attributeName="doc:name" whereValue="#['DOCNAME']" />
            </munit-tools:with-attributes>
            <munit-tools:then-return>
                <munit-tools:payload mediaType="application/json"
                    value="#[MunitTools::getResourceAsString('testPayloadB.json')]" />
            </munit-tools:then-return>
        </munit-tools:mock-when>

基本上,对于至少 4 种情况,模拟具有相同的结构,在这些情况下,我试图评估根据来自名为 DOCNAME 的 Http 请求的负载内容而变化的行为。事实上,这个请求包含在缓存范围内。这意味着如果我尝试单独 运行 个测试用例,它们会成功,但是当所有套件都是 运行 时,第一个用例执行正常但后续的失败。

有没有人知道应该如何以不影响所有测试套件的方式模拟来自缓存范围请求的响应负载?

所以我猜测试失败了,因为之前的值被缓存了。这可能取决于缓存策略,并且可能每次测试使用不同的数据,因此缓存总是未命中。

但如果您需要使用类似的数据,您可以在每次测试前清除缓存。例如:

鉴于此流程,设置一个命名对象存储和使用所述对象存储的缓存策略:

<os:object-store name="myObjectStore" doc:name="Object store"
        doc:id="25cec35b-2680-4754-8073-633f3e60a34f" />
    <ee:object-store-caching-strategy
        name="Caching_Strategy" objectStore="myObjectStore" doc:name="Caching Strategy"
        doc:id="fd059f4f-d3be-4971-a36c-374506e2db49" />

    <flow name="httpCache">
        <ee:cache cachingStrategy-ref="Caching_Strategy"  doc:name="Cache"  doc:id="0dcabdb4-d6fc-41d5-aebd-77d015e02dd7" >
            <http:request method="GET" doc:name="Request" doc:id="366184e9-6c0b-41d4-8414-3dfdd047ecb2" url="http://google.com"/>
            <logger level="ERROR" message="CACHE MISS" />
        </ee:cache>
        <logger level="ERROR" message="After cache: #[payload]" />   
    </flow>

然后是您的测试,设置一个之前的操作来清除该存储:

<munit:before-test name="clearOS" description="clears OS">
            <os:clear objectStore="myObjectStore" doc:id="cd931ce7-6945-4dc9-919a-3ff9b158d746" />
    </munit:before-test>

    <munit:test name="new-test-suite-httpCacheTest" description="Test" doc:id="b8e299ea-d191-4c0a-ac71-27a12b28d275" >
        <munit:behavior >
            <munit-tools:mock-when doc:name="Mock when" doc:id="8bf1468d-c9c1-48a1-9009-bae02bf8e788" processor="http:request">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute attributeName="doc:name" whereValue="Request" />
                </munit-tools:with-attributes>
                <munit-tools:then-return >  
                     <munit-tools:payload value="#['mockPayload']"/>
                </munit-tools:then-return>
            </munit-tools:mock-when>
        </munit:behavior>
        <munit:execution >
            <flow-ref doc:name="Flow-ref to httpCache" doc:id="2dd44c7b-f250-4852-9e53-f4eee3b5ad84" name="httpCache"/>
        </munit:execution>
        <munit:validation >
            <munit-tools:assert-that doc:name="Assert that" doc:id="6521459d-04dd-4e59-8a2e-16a27f51c091" expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
        </munit:validation>
    </munit:test>

    <munit:test name="new-test-suite-httpCacheTest2" description="Test" doc:id="d6ea0d83-f943-450e-948c-26596bf41207" >
        <munit:behavior >
            <munit-tools:mock-when doc:name="Mock when" doc:id="8688876f-144e-4f7f-8ef0-98a8e6caeca0" processor="http:request">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute attributeName="doc:name" whereValue="Request" />
                </munit-tools:with-attributes>
                <munit-tools:then-return >  
                     <munit-tools:payload value="#['mockPayload2']"/>
                </munit-tools:then-return>
            </munit-tools:mock-when>
        </munit:behavior>
        <munit:execution >
            <flow-ref doc:name="Flow-ref to httpCache" doc:id="b4a1d37a-f0e4-4fa1-9c78-084ed3f6faf8" name="httpCache"/>
        </munit:execution>
        <munit:validation >
            <munit-tools:assert-that doc:name="Assert that" doc:id="46febad5-d557-4152-829e-db61aa6ef409" expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
        </munit:validation>
    </munit:test>

这两个都会记录:

LoggerMessageProcessor: CACHE MISS

然后:

LoggerMessageProcessor: After cache: mockPayload

LoggerMessageProcessor: After cache: mockPayload2

因为,如果没有 os:clear,它只会第一次错过缓存,并从缓存中打印出相同的有效负载:

 LoggerMessageProcessor: CACHE MISS
 LoggerMessageProcessor: After cache: mockPayload

 LoggerMessageProcessor: After cache: mockPayload

或者,您可以将缓存和 http 处理器移动到它们自己的流中并模拟该流,这样缓存就永远不会在您的测试中使用。