JSON文字列である本文のフィールド値を で確認しようとしていますJsonPathExpression
。
以下の例でJsonPathExpression
は、ルート JSON オブジェクトに " " という名前のフィールドがあるかどうかがチェックされtype
ます。私が達成したいのはJsonPathExpression
、フィールド値 " type
" が特定の文字列値に等しい場合を使用してアサートすることです。
注:メッセージ本文を抽出する方法が他にもあることは知っていますが、MockEndpoint#getReceivedExchanges
それはアサーションの範囲外であるため、使用したくありません。
これが私のテストクラスです。
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.language.JsonPathExpression;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class MockTestWithJsonPathExpression extends CamelTestSupport {
@Test
public void testMessageContentWithJSONPathExpression() throws InterruptedException {
MockEndpoint mock = getMockEndpoint("mock:quote");
mock.expectedMessageCount(1);
mock.message(0).body().matches(new JsonPathExpression("$.type")); // how to test the content of the value
/* Json string;
*
* {
* "test": "testType"
* }
*
*/
String body = "{\"type\": \"testType\"}";
template.sendBody("stub:jms:topic:quote", body);
assertMockEndpointsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("stub:jms:topic:quote")
.to("mock:quote");
}
};
}
}