title: How to mock - reading file from s3
body: <p>I am new to writing unit tests. I am trying to read a JSON file stored in <code>S3</code> and I am getting an "Argument passed to when() is not a mock!" and "profile file cannot be null" error.</p>
  
  <p>This is what <strong>I have tried</strong> so far <a href="https://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html" rel="noreferrer">Retrieving Object Using JAVA</a>:</p>
  
  <pre class="lang-java prettyprint-override"><code>private void amazonS3Read() {
      String clientRegion = "us-east-1";
      String bucketName = "version";
      String key = "version.txt";
      S3Object fullObject = null;
      try {
          AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
              .withRegion(clientRegion)
              .withCredentials(new ProfileCredentialsProvider())
              .build();
          fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
          S3ObjectInputStream s3is = fullObject.getObjectContent();
          json = returnStringFromInputStream(s3is);
          fullObject.close();
          s3is.close();
      } catch (AmazonServiceException e) {
          // The call was transmitted successfully, but Amazon S3 couldn't process
          // it, so it returned an error response.
          e.printStackTrace();
      } catch (SdkClientException e) {
          // Amazon S3 couldn't be contacted for a response, or the client
          // couldn't parse the response from Amazon S3.
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      //Do some operations with the data
  }
  </code></pre>
  
  <p><strong>Test File</strong></p>
  
  <pre class="lang-java prettyprint-override"><code> @Test
   public void amazonS3ReadTest() throws Exception {
       String bucket = "version";
       String keyName = "version.json";
       InputStream inputStream = null;
       S3Object s3Object = Mockito.mock(S3Object.class);
       GetObjectRequest getObjectRequest = Mockito.mock(GetObjectRequest.class);
  
       getObjectRequest = new GetObjectRequest(bucket, keyName);
  
       AmazonS3 client = Mockito.mock(AmazonS3.class);
       Mockito.doNothing().when(AmazonS3ClientBuilder.standard());
       client = AmazonS3ClientBuilder.standard()
           .withRegion(clientRegion)
           .withCredentials(new ProfileCredentialsProvider())
           .build();
  
       Mockito.doReturn(s3Object).when(client).getObject(getObjectRequest);
       s3Object = client.getObject(getObjectRequest);
  
       Mockito.doReturn(inputStream).when(s3Object).getObjectContent();
       inputStream = s3Object.getObjectContent();
       //performing other operations
   }
  </code></pre>
  
  <p><strong>Getting two different exceptions:</strong></p>
  
  <pre><code>Argument passed to when() is not a mock! Example of correct stubbing: doThrow(new RuntimeException()).when(mock).someMethod();
  
  org.mockito.exceptions.misusing.NotAMockException: 
  Argument passed to when() is not a mock!
  Example of correct stubbing: 
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>profile file cannot be null
  
  java.lang.IllegalArgumentException: profile file cannot be null
  at com.amazonaws.util.ValidationUtils.assertNotNull(ValidationUtils.java:37)
  at com.amazonaws.auth.profile.ProfilesConfigFile.&lt;init&gt;(ProfilesConfigFile.java:142)
  at com.amazonaws.auth.profile.ProfilesConfigFile.&lt;init&gt;(ProfilesConfigFile.java:133)
  at com.amazonaws.auth.profile.ProfilesConfigFile.&lt;init&gt;(ProfilesConfigFile.java:100)
  at com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:135)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1184)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:774)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:726)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:719)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:701)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:669)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:651)
  at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:515)
  at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4443)
  at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4390)
  at com.amazonaws.services.s3.AmazonS3Client.getObject(AmazonS3Client.java:1427)
  </code></pre>
  
  <p>What am I doing wrong and how do I fix this?</p>