我想模擬Java中DataClient類的對象。我不知道如何在這裏模擬s3成員變量。我來自ruby背景,我們有一些名爲rspec-mock的地方,我們不需要模擬實例變量。如何根據服務調用模擬私有成員變量
public class DataClient {
private String userName, bucket, region, accessKey, secretKey;
private AmazonS3Client s3;
public OdwClient(String accessKey, String secretKey, String userName, String bucket, String region){
this.accessKey = accessKey;
this.accessKey = secretKey;
this.userName = userName;
this.bucket = bucket;
this.region = region;
this.s3 = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
}
public boolean pushData(String fileName) {
s3.putObject(new PutObjectRequest("bucketName", fileName, new File("filePath")).
return true;
}
}
所有我現在已經嘗試在測試是:
@Before
public void setUp() throws Exception{
DataClient client = Mockito.mock(DataClient.class);
}
@Test
public void testPushData() {
// I don't know how to mock s3.putObject() method here
}
我的測試中不斷失敗。
難道你不是指** PowerMockito.whenNew **? –
@IgorGanapolsky你是對的。更新了答案。 – ccstep1