use of org.mockito.internal.stubbing.answers.ThrowsException in project mobile-center-sdk-android by Microsoft.
the class StorageHelperTest method readFileNotFound.
@Test
public void readFileNotFound() throws Exception {
mockStatic(MobileCenterLog.class);
FileReader fileReader = mock(FileReader.class, new ThrowsException(new FileNotFoundException()));
whenNew(FileReader.class).withAnyArguments().thenReturn(fileReader);
assertNull(StorageHelper.InternalStorage.read(new File("")));
verify(fileReader).close();
verifyStatic();
MobileCenterLog.error(anyString(), anyString(), any(IOException.class));
}
use of org.mockito.internal.stubbing.answers.ThrowsException in project hadoop by apache.
the class TestDFSClientRetries method testNotYetReplicatedErrors.
// more tests related to different failure cases can be added here.
/**
* Verify that client will correctly give up after the specified number
* of times trying to add a block
*/
@SuppressWarnings({ "serial", "unchecked" })
@Test
public void testNotYetReplicatedErrors() throws IOException {
final String exceptionMsg = "Nope, not replicated yet...";
// Allow one retry (total of two calls)
final int maxRetries = 1;
conf.setInt(HdfsClientConfigKeys.BlockWrite.LOCATEFOLLOWINGBLOCK_RETRIES_KEY, maxRetries);
NamenodeProtocols mockNN = mock(NamenodeProtocols.class);
Answer<Object> answer = new ThrowsException(new IOException()) {
int retryCount = 0;
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
retryCount++;
System.out.println("addBlock has been called " + retryCount + " times");
if (// First call was not a retry
retryCount > maxRetries + 1)
throw new IOException("Retried too many times: " + retryCount);
else
throw new RemoteException(NotReplicatedYetException.class.getName(), exceptionMsg);
}
};
when(mockNN.addBlock(anyString(), anyString(), any(ExtendedBlock.class), any(DatanodeInfo[].class), anyLong(), any(String[].class), Matchers.<EnumSet<AddBlockFlag>>any())).thenAnswer(answer);
Mockito.doReturn(new HdfsFileStatus(0, false, 1, 1024, 0, 0, new FsPermission((short) 777), "owner", "group", new byte[0], new byte[0], 1010, 0, null, (byte) 0, null)).when(mockNN).getFileInfo(anyString());
Mockito.doReturn(new HdfsFileStatus(0, false, 1, 1024, 0, 0, new FsPermission((short) 777), "owner", "group", new byte[0], new byte[0], 1010, 0, null, (byte) 0, null)).when(mockNN).create(anyString(), (FsPermission) anyObject(), anyString(), (EnumSetWritable<CreateFlag>) anyObject(), anyBoolean(), anyShort(), anyLong(), (CryptoProtocolVersion[]) anyObject());
final DFSClient client = new DFSClient(null, mockNN, conf, null);
OutputStream os = client.create("testfile", true);
// write one random byte
os.write(20);
try {
os.close();
} catch (Exception e) {
assertTrue("Retries are not being stopped correctly: " + e.getMessage(), e.getMessage().equals(exceptionMsg));
}
}
Aggregations