在柑橘模拟器中提取帖子变量

时间:2018-08-01 13:25:44

标签: java spring-rest citrus-framework

我正在尝试柑橘模拟器并尝试一个休息的例子。下面是模拟器和Rest Client的代码。关键是我没有使用@CitrusTest注释。

@Scenario("MyRestServiceScenario")
@RequestMapping(value = "/services/rest/simulator/hello", method = RequestMethod.POST)
public class MyRestServiceSimulator extends AbstractSimulatorScenario {
    @Override
    public void run(ScenarioDesigner scenario) {

        scenario
                .http()
                .receive()
                .post()
                .payload("{\"Hello\":[\"JS01\"]}")
                .extractFromPayload("$Hello", "greeting").getActor().;

        //String gr = scenario.getTestContext().getVariable("greeting");
        scenario.echo("Received greeting: ${greeting}");

        scenario
                .http()
                .send()
                .response(HttpStatus.OK)
                .payload("<HelloResponse xmlns=\"http://citrusframework.org/schemas/hello\">" +
                        "Hi there!" +
                        "</HelloResponse>");
    }
}

主类

public class CitrusRest {
    public static void main(String... args) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        RestTemplate rt = new RestTemplate();

        MultiValueMap<String, String>  vars = new LinkedMultiValueMap<String, String>();
        vars.add("Hello", "JS01");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(vars, headers);
        String uri = "http://" + "localhost" + ":8080//services/rest/simulator/hello";

        rt.postForEntity(uri, request, String.class);
    }
}

在调试器模式下,我确实看到有效负载到达了run方法,并且我希望该帖子.extractFromPayload(“ $ Hello”,“ greeting”)。我应该在问候变量中收到JS01。但是变量从未初始化。

有人可以在这里帮忙吗?

1 个答案:

答案 0 :(得分:1)

$.Hello不是有效的JsonPath表达式。您还应该在日志中看到一些错误:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreateView(inflater, container, savedInstanceState);
    // Initialize the scanner first so we can track the current context
    MobileBarcodeScanner.Initialize(Activity.Application);
    //Create a new instance of our Scanner
    scanner = new MobileBarcodeScanner();
    var view = this.BindingInflate(Resource.Layout.surface_qrcode, null);   
    Task.Run(() => ScanQRCode());

    return view;
}

void ScanQRCode()
{
    Task.Run(() =>
    {
    Activity.RunOnUiThread(async () =>
    {
        // Update the data fetched/calculated on the UI thread;
        scanner.UseCustomOverlay = true;       
        //Inflate our custom overlay from a resource layout
        zxingOverlay = LayoutInflater.FromContext(Activity).Inflate(Resource.Layout.surface_qrcode, null);

        //Set our custom overlay
        scanner.CustomOverlay = zxingOverlay;

        //Start scanning!
        var result = await scanner.Scan(new MobileBarcodeScanningOptions { AutoRotate = true });        
        HandleScanResult(result);
    });
    }).Wait();

}  

正确的表达式应该是{{1}}