jeudi 26 mai 2016

How to mock private member variable depending on a service call

I want to mock an object of DataClient class in Java. I am not sure how to mock s3 member variable here. I came from ruby background and we have something called rspec-mock where we don't need to mock instance variables.

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;
  }
}

All I have tried now is in tests is:

    @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
    }

My tests keeps failing.

Aucun commentaire:

Enregistrer un commentaire