如何比较 *.png 文件与空手道中 Java 方法调用的结果

How to compare *.png file with result of Java method call in Karate

我有测试环境:[REST API]-->[RabbitMQ] 并尝试将文件(包含多部分)发送到 REST API 并直接从 RabbitMQ 读取:

* bytes expectedPayload = read('flask.png')
And multipart file file = { read: 'flask.png', contentType: 'application/json' }
And multipart field message = { messageFlowName: 'TestMSGFlow', moduleInstanceId: 5 }
When method POST
Then status 200
 ...
* bytes receivedPayload = amqpConnection.getMessagePayload('test.rabbitmq.queue', 'testChannel')
And match receivedPayload == read('flask.png')

amqpConnection.getMessagePayload方法:

public byte[] getMessagePayload(String queueName, Channel channel)  {
    byte[] message = null;

    try {
        GetResponse response = channel.basicGet(queueName, true);
        if (response == null) {
            System.out.println("DEBUG: No message found.");
        } else {
            AMQP.BasicProperties props = response.getProps();
            return response.getBody();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return message;
}

我得到了结果:

actual: [B@64b70919, expected: [B@4e31c3ec, reason: actual and expected byte-arrays are not equal

我试过使用 *.json 而不是 *.png(作为测试文件)并且效果很好。如何使其与 *.png 一起使用?

它应该可以工作,所以看起来您在从通道/消息中提取字节数组的例程中出错了。

可能涉及一些编码/解码?

你可以做个简单的测试:

* bytes temp = read('flask.png')
* match temp == read('flask.png')