public void GetCredentials2() { // When Credentials is Expired // Mock Response var response = new HttpResponse(); var content = Encoding.GetEncoding("UTF-8").GetBytes( "{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + DateTimeMock.getExpiredDateTimeString() + "\"}"); response.ContentType = FormatType.JSON; response.Content = content; response.Status = 200; // Mock Fetcher var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher> { CallBase = true }; mockFetcher.Setup(foo => foo.GetResponse( It.IsAny <HttpRequest>() )).Returns(response); var fetcher = mockFetcher.Object; var roleName = ACKMock.GetRoleName(true); var instance = new InstanceProfileCredentialsProvider(roleName); AlibabaCloudCredentialsProvider provider = instance; instance.withFetcher(fetcher); // Throw exception if the date is invalid Assert.Throws <ClientException>(() => { var credentials = provider.GetCredentials(); }); }