Search in sources :

Example 16 with ThrowsException

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));
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with ThrowsException

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));
    }
}
Also used : CreateFlag(org.apache.hadoop.fs.CreateFlag) NamenodeProtocols(org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) CryptoProtocolVersion(org.apache.hadoop.crypto.CryptoProtocolVersion) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) ExtendedBlock(org.apache.hadoop.hdfs.protocol.ExtendedBlock) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) ChecksumException(org.apache.hadoop.fs.ChecksumException) FileNotFoundException(java.io.FileNotFoundException) NotReplicatedYetException(org.apache.hadoop.hdfs.server.namenode.NotReplicatedYetException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) RemoteException(org.apache.hadoop.ipc.RemoteException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) Matchers.anyObject(org.mockito.Matchers.anyObject) FsPermission(org.apache.hadoop.fs.permission.FsPermission) RemoteException(org.apache.hadoop.ipc.RemoteException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)17 IOException (java.io.IOException)8 File (java.io.File)4 FileReader (java.io.FileReader)4 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStream (java.io.OutputStream)3 MockitoException (org.mockito.exceptions.base.MockitoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 Invocation (org.mockito.invocation.Invocation)2 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)2 Neo4jJsonCodecTest (org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)2 Closeable (java.io.Closeable)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CryptoProtocolVersion (org.apache.hadoop.crypto.CryptoProtocolVersion)1 ChecksumException (org.apache.hadoop.fs.ChecksumException)1