[英]how to compare two bytearrayinputstream in junit testing?
While comparing two bytearrayinputstream, i am facing the below errors for the below given function 在比较两个bytearrayinputstream时,我面临以下给定函数的以下错误
code here 这里的代码
@Test
public void fileIsSame() throws IOException, InvalidEndpointException, InvalidPortException, InvalidKeyException, InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, NoResponseException, ErrorResponseException, InternalException, InvalidArgumentException, XmlPullParserException {
FileInputStream file = new FileInputStream("/Users/gauris/Documents/samplephoto/pexels-photo-46710.jpeg");
String fileName = "pexels-photo-46710.jpeg";
ByteArrayInputStream uploadByteArrayInputStream = new ByteArrayInputStream(file.toString().getBytes());
minioClient.putObject(bucketName, fileName, uploadByteArrayInputStream, uploadByteArrayInputStream.available(), "application/octet-stream");
String actualresult = IOUtils.toString(uploadByteArrayInputStream, StandardCharsets.UTF_8);
InputStream stream = minioClient.getObject(bucketName, fileName);
byte currentXMLBytes[] = stream.toString().getBytes(); //convert inputStream to byteArrayInputStream;
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(currentXMLBytes);
String Expectedresult = IOUtils.toString(stream, StandardCharsets.UTF_8);
System.out.println("expected: "+Expectedresult);
System.out.println("actual: "+ actualresult);
//Arrays.equals((uploadByteArrayInputStream).toByteArray(), (byteInputStream).toByteArray());
assertEquals(byteInputStream.toString(), uploadByteArrayInputStream.toString());
uploadByteArrayInputStream.close();
stream.close();
}
Errors 错误
org.junit.ComparisonFailure: expected:<...yteArrayInputStream@[43195e57]> but was:<...yteArrayInputStream@[333291e3]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144)
To String method prints a human readable representation of the a given object, and not the content of the stream. To String方法打印给定对象的人类可读表示形式,而不是流的内容。 You must fist obtain the strings from the Streams and then compare them.
您必须首先从Streams中获取字符串,然后进行比较。
If you use Java 9 then, you can, 如果您使用Java 9,则可以,
new String(input.readAllBytes(), StandardCharsets.UTF_8);
Or the old way, 还是老办法
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
return result.toString("UTF-8");
and then you can compare the Strings. 然后您可以比较字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.