我有一种方法可以将文件上传到Amazon S3。我正在尝试为此方法编写JUnit,但在S3AsyncClient上获取NullPointerException:
我的班级:
public class S3Client<T> {
private static final Logger log = LoggerFactory.getLogger(S3Client.class);
S3AsyncClient client;
/**
*
* @param s3Configuration
*/
public S3Client(AWSS3Configuration s3Configuration) {
this.client = s3Configuration.getAsyncClient();
}
/**
* Uploads a file s3 bucket and returns etag
* @param uploadData
* @return
* @throws S3Exception
*/
public CompletableFuture<String> uploadFile(S3UploadData<T> uploadData) throws S3Exception {
int contentLength;
AsyncRequestBody asyncRequestBody;
if(uploadData.getContent() instanceof String) {
String content = (String) uploadData.getContent();
contentLength = content.length();
asyncRequestBody = AsyncRequestBody.fromString(content);
}
else if(uploadData.getContent() instanceof byte[]){
byte[] bytes = (byte[]) uploadData.getContent();
contentLength = bytes.length;
asyncRequestBody = AsyncRequestBody.fromBytes(bytes);
}
else{
throw new IllegalArgumentException("Unsupported upload content type");
}
PutObjectRequest putObjRequest = PutObjectRequest.builder()
.bucket(uploadData.getBucketName())
.key(uploadData.getFileName())
.metadata(uploadData.getMetaData())
.contentLength((long) contentLength).build();
CompletableFuture<String> response = client.putObject(putObjRequest, asyncRequestBody).thenApply(
getPutObjectResponse -> {
log.info("Got response from S3 upload={}", getPutObjectResponse.eTag());
return getPutObjectResponse.eTag();
});
response.exceptionally(throwable -> {
log.error("Exception occurred while uploading a file intuit_tid={} file={}",uploadData.getTransactionId(),uploadData.getFileName());
throw new S3Exception(throwable.getMessage());
});
return response;
}
S3UploadData类对象的输入:`
@Getter@allargsconstuctor
public class InputData<T> {
T content;
String fileName;
String bucketName;
String transactionId;
Map<String, String> metaData;
}`
你能帮我写Junit for uploadFile方法吗?
您没有JUNIT测试代码。你应该有使用组织的代码。朱尼特。木星api*
不要使用模拟,而是在@TestInstance集成测试中调用实际的S3异步代码,以确保其正常工作。例如,这是我在IntelliJ中的测试。
正如你所见,我的测试通过了,我知道我的代码可以工作——这就是AWS集成测试的重点。
如果我的代码失败或由于某种原因引发异常,我的测试将失败。例如,如果我传递了一个不存在的桶名,我将得到:
以下是我的Java Amazon S3异步代码:
package com.example.s3.async;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
// snippet-end:[s3.java2.async_ops.import]
// snippet-start:[s3.java2.async_ops.main]
/**
* To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class S3AsyncOps {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
" S3AsyncOps <bucketName> <key> <path>\n\n" +
"Where:\n" +
" bucketName - the name of the Amazon S3 bucket (for example, bucket1). \n\n" +
" key - the name of the object (for example, book.pdf). \n" +
" path - the local path to the file (for example, C:/AWS/book.pdf). \n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String key = args[1];
String path = args[2];
Region region = Region.US_WEST_2;
S3AsyncClient client = S3AsyncClient.builder()
.region(region)
.build();
putObjectAsync(client, bucketName, key, path);
}
public static void putObjectAsync(S3AsyncClient client,String bucketName, String key, String path) {
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
// Put the object into the bucket
CompletableFuture<PutObjectResponse> future = client.putObject(objectRequest,
AsyncRequestBody.fromFile(Paths.get(path))
);
future.whenComplete((resp, err) -> {
try {
if (resp != null) {
System.out.println("Object uploaded. Details: " + resp);
} else {
// Handle error
err.printStackTrace();
}
} finally {
// Only close the client when you are completely done with it
client.close();
}
});
future.join();
}
}
现在,对于我的测试,我想调用这个代码,而不是模拟它。我已经在IntelliJ中设置了这样的测试,
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import software.amazon.awssdk.regions.Region;
import java.io.*;
import java.util.*;
import com.example.s3.async.*;
import software.amazon.awssdk.services.s3.S3AsyncClient;
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AmazonS3AsyncTest {
private static S3AsyncClient s3AsyncClient;
// Define the data members required for the tests
private static String bucketName = "";
private static String objectKey = "";
private static String objectPath = "";
private static String toBucket = "";
@BeforeAll
public static void setUp() throws IOException {
// Run tests on Real AWS Resources
s3AsyncClient = S3AsyncClient.builder()
.region(Region.US_EAST_1)
.build();
try (InputStream input = AmazonS3Test.class.getClassLoader().getResourceAsStream("config.properties")) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find config.properties");
return;
}
//load a properties file from class path, inside static method
prop.load(input);
// Populate the data members required for all tests
bucketName = prop.getProperty("bucketName");
objectKey = prop.getProperty("objectKey");
objectPath= prop.getProperty("objectPath");
toBucket = prop.getProperty("toBucket");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Test
@Order(1)
public void whenInitializingAWSS3Service_thenNotNull() {
assertNotNull(s3AsyncClient);
System.out.println("Test 1 passed");
}
@Test
@Order(2)
public void putObject() {
S3AsyncOps.putObjectAsync(s3AsyncClient, bucketName, objectKey, objectPath);
System.out.println("Test 2 passed");
}
}
但是对于这个源代码,我不能编写JUnit测试,因为不能模拟类。对于模拟final类,我们可以使用PowerMock,它支持模拟静态和final方法。但在这里,如果我使用PowerMock,它仍然不是嘲弄。我使用的是Spring Framework5.2.1.版本,这个版本的JUnit有没有什么变化来模拟最终类或方法?或者任何一个可以帮助我编写此代码的单元测试(我使用的版本是Spring Frame
问题内容: 我有一个Java命令行程序。我想创建JUnit测试用例以进行模拟。因为当我的程序运行时,它将进入while循环并等待用户输入。如何在JUnit中模拟呢? 问题答案: 从技术上讲,可以进行切换,但是总的来说,不直接在代码中调用它,而是添加一层间接层,这样输入源就可以从应用程序的某个位置进行控制,这样会更健壮。确切地讲,这是实现的详细信息-依赖项注入的建议很好,但是你不一定需要引入第三方框
我有一个Spring组件我想测试,这个组件有一个autowired属性,为了单元测试的目的,我需要更改它。问题是,这个类在后构造方法中使用autowired组件,所以我不能在它实际使用之前替换它(即通过反射)。 我该怎么做? 在调用postconstruct方法之前,有什么方法可以用其他东西替换资源吗?喜欢告诉Spring JUnit runner autowire不同的实例吗?
问题内容: 我在源代码中使用了BufferedWriter对象 我正在尝试在我的测试用例中模拟它,如下所示: 但是,BufferedWriter不会被嘲笑,它总是进入实际的实现中。是因为它不能模拟BufferedWriter,因为它是一个具体的类吗?这是否意味着无法模拟任何java.io类?有没有办法模拟它,或者我做错了什么? 问题答案: ,你可以嘲笑的Java IO类(包括它们的构造,所以未来的
我有一个服务我是这样嘲笑的: } 服务: 我需要能够模拟“CloseableHttpResponse Response = http client . execute(request,clientContext)”,这样“response”对象就是我提前创建的。我希望一些嘲讽的when/then构造可以解决这个问题。我会很感激如何做到这一点的想法。谢谢!
我完全被困在java测试中;它是关于通过测试方法将字符'a'发送到JFrame组件的JTextField。 JFrame类实现KeyListener接口,并以此重写KeyPressed、KeyTyped和KeyReleased。同时,我将JTextField的所有按键转移到JFrame;在JFrame构造函数中,我有: 我想测试这种行为,然后模拟JTextField中类型a字符的操作。 我还尝试了