如何测试在内部调用私有方法的公共方法


/*
this method will create the required manifest file in compatible format such that
quicksight can import data from specified s3 bucket
*/
private JSONObject CreateManifestFileJSONObject(JSONObject ManifestFile){
JSONArray URIPrefixArray= new JSONArray();
URIPrefixArray.put(PrefixLocation);
JSONObject URIPrefixJSONObject= new JSONObject();
URIPrefixJSONObject.put("URIPrefixes",URIPrefixArray);
JSONArray FileLocationsArray= new JSONArray();
FileLocationsArray.put(URIPrefixJSONObject);
JSONObject globalUploadSettings= new JSONObject();
globalUploadSettings.put("format","JSON");
ManifestFile.put("globalUploadSettings",globalUploadSettings);
ManifestFile.put("fileLocations",FileLocationsArray);
return(ManifestFile);
}
/*
this method will upload the ManifestFile to same S3 Bucket in which data files is stored
*/
private void UploadManifestFileJSONObjectToS3(JSONObject ManifestFile){
try {
AmazonS3 S3Client = new Utility().SetUpS3Client();
byte[] fileContentBytes = (ManifestFile.toString()).getBytes();
InputStream fileInputStream = new ByteArrayInputStream(fileContentBytes);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(fileContentBytes.length);
S3Client.putObject(new PutObjectRequest(Bucket_Name, ManifestFileName, fileInputStream, objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
}
catch(Exception exception){
exception.printStackTrace();
}
}
public void handler() {
System.out.println("inside the manifest file");
try {
JSONObject ManifestFile = new JSONObject();
ManifestFile = CreateManifestFileJSONObject(ManifestFile);
UploadManifestFileJSONObjectToS3(ManifestFile);
}
catch(Exception exception){
exception.printStackTrace();
}

我想测试处理程序方法,但 handlor 方法正在调用私有方法,所以我不知道如何为此代码编写测试类。 我想为这个类编写单元测试,请帮忙

这是我到目前为止能够创建的测试类,但它不一定会模拟 s3 行为

@Test
public void handler() {
ManifestFileHandler manifestFileHandler=new ManifestFileHandler();
manifestFileHandler.handler();
}

您可以在 PowerMock 提供使用反射来执行某些操作的实用程序的帮助下测试私有方法。 检查以下示例, https://examples.javacodegeeks.com/core-java/mockito/mockito-mock-private-method-example-with-powermock/

关于单元测试私有函数有两种思想流派。首先是将它们设为公共(或受保护或包可访问(,并像测试公共函数一样测试它们。第二个是,如果它们是私有的,它们是封装的实现细节的一部分,你只需要通过公共函数测试它们。

我个人的观点是,复杂的私有函数通常表明你正在打破单一责任原则,并且很可能你应该在私有函数中具有逻辑,这些逻辑应该被拆分成一个单独的类,然后可以通过其公共方法进行测试。

关于你发布的代码,你有一个比如何测试私有函数更大的问题:你的类依赖于你无法控制的其他类。您无法模拟这些类的行为来测试各种方案或验证它们是否已正确调用。我怀疑正是这个问题真正落后于你的问题。

例如,我建议您在类中注入S3Client,而不是通过new Utility().SetUpS3Client()在内部创建它。这样,您可以模拟其行为并验证代码是否正确调用了它。尝试使用此类的真实版本执行此操作将具有挑战性。

因此,使用此模型,您的代码可能如下所示:

public class ManifestFileHandler {
private final S3Client client;
public ManifestFileHandler(S3Client client) {
this.client = client;
}
private void upload(JSONObject manifestFile) {
...
client.putObject(...);
}
public void handleManifest() {
...
upload(manifestFile);
...
}

}

和你的测试代码(使用模拟(:

@Test
void testManifestUpload() {
S3Client client = mock(S3Client.class);
ManifestFileHandler handler = new ManifestFileHandler(client);
handler.handleManifest();
verify(client).putObject(expectedObject);
}

如果您需要捕获传递给putObject的论点并断言它的各个方面,那么大多数模拟工具(包括mockito(都可以这样做,但超出了您的问题范围。

相关内容

最新更新