我需要测试这个函数,因为错误"" Width (0) and height (0) cannot be <= 0 "。函数是这样的:
public class StreamImage {
private static final int MAX_WIDTH = 100 ;
private static final int MAX_HEIGHT = 100;
private AmazonS3 s3Client;
public StreamImage(AmazonS3 s3Client) {
this.s3Client = s3Client;
}
public InputStream getStreamImage(String srcBucket, String srcKey){
S3Object s3Object = s3Client.getObject(new GetObjectRequest(
srcBucket, srcKey));
return s3Object.getObjectContent();
}
public Contenitore rielaborationImmage(String srcBucket, String srcKey, String imageType) throws IOException {
InputStream objectData = getStreamImage(srcBucket, srcKey);
BufferedImage srcImage = ImageIO.read(objectData);
int srcHeight = srcImage.getHeight();
int srcWidth = srcImage.getWidth();
float scalingFactor = Math.min(MAX_WIDTH / srcWidth, MAX_HEIGHT / srcHeight);
int width = (int) (scalingFactor * srcWidth);
int height = (int) (scalingFactor * srcHeight);
BufferedImage resizedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
// Fill with white before applying semi-transparent (alpha) images
g.setPaint(Color.white);
g.fillRect(0, 0, width, height);
// Simple bilinear resize
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImage, 0, 0, width, height, null);
g.dispose();
// Re-encode image to target format
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(resizedImage, imageType, os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
// Set Content-Length and Content-Type
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(os.size());
if ("jpeg".equals(imageType)) {
meta.setContentType("image/jpeg");
}
if ("png".equals(imageType)) {
meta.setContentType("image/png");
}
Contenitore is_n= new Contenitore( is, meta);
return is_n;
}
}
我想用 Junit 测试它,但我必须模拟函数“getStreamImage”,我该怎么做?还有其他方法可以测试吗?