Java Code Examples for org.mockito.stubbing.OngoingStubbing#thenThrow()

The following examples show how to use org.mockito.stubbing.OngoingStubbing#thenThrow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GPDBWritableTest.java    From pxf with Apache License 2.0 5 votes vote down vote up
private DataInput buildStream(int[] data, boolean throwException) throws Exception {
    inputStream = mock(DataInput.class);
    OngoingStubbing<Integer> ongoing = when(inputStream.readInt());
    for (int b : data) {
        ongoing = ongoing.thenReturn(b);
    }

    if (throwException) {
        ongoing.thenThrow(new EOFException());
    }
    return inputStream;
}
 
Example 2
Source File: AMQPObservableQueueTest.java    From conductor with Apache License 2.0 5 votes vote down vote up
Channel mockChannelForQueue(Channel channel, boolean isWorking, boolean exists, String name,
		List<GetResponse> queue) throws IOException {
	// queueDeclarePassive
	final AMQImpl.Queue.DeclareOk queueDeclareOK = new AMQImpl.Queue.DeclareOk(name, queue.size(), 1);
	if (exists) {
		Mockito.when(channel.queueDeclarePassive(eq(name))).thenReturn(queueDeclareOK);
	} else {
		Mockito.when(channel.queueDeclarePassive(eq(name))).thenThrow(new IOException("Queue " + name + " exists"));
	}
	// queueDeclare
	OngoingStubbing<AMQP.Queue.DeclareOk> declareOkOngoingStubbing = Mockito.when(channel.queueDeclare(eq(name),
			Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap()))
			.thenReturn(queueDeclareOK);
	if (!isWorking) {
		declareOkOngoingStubbing.thenThrow(new IOException("Cannot declare queue " + name),
				new RuntimeException("Not working"));
	}
	// messageCount
	Mockito.when(channel.messageCount(eq(name))).thenReturn(1l * queue.size());
	// basicGet
	OngoingStubbing<String> getResponseOngoingStubbing = Mockito
			.when(channel.basicConsume(eq(name), Mockito.anyBoolean(), (Consumer) Mockito.any(Consumer.class))).thenAnswer(new ReturnsElementsOf(queue));
	if (!isWorking) {
		getResponseOngoingStubbing.thenThrow(new IOException("Not working"), new RuntimeException("Not working"));
	}
	// basicPublish
	if (isWorking) {
		Mockito.doNothing().when(channel).basicPublish(eq(StringUtils.EMPTY), eq(name),
				Mockito.any(AMQP.BasicProperties.class), Mockito.any(byte[].class));
	} else {
		Mockito.doThrow(new IOException("Not working")).when(channel).basicPublish(eq(StringUtils.EMPTY), eq(name),
				Mockito.any(AMQP.BasicProperties.class), Mockito.any(byte[].class));
	}
	return channel;
}
 
Example 3
Source File: RdeUploadActionTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private static JSch createThrowingJSchSpy(JSch jsch, int numTimesToThrow) throws JSchException {
  JSch jschSpy = spy(jsch);
  OngoingStubbing<Session> stubbing =
      when(jschSpy.getSession(anyString(), anyString(), anyInt()));
  for (int i = 0; i < numTimesToThrow; i++) {
    stubbing = stubbing.thenThrow(new JSchException("The crow flies in square circles."));
  }
  stubbing.thenCallRealMethod();
  return jschSpy;
}
 
Example 4
Source File: BaseStubbing.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public OngoingStubbing<T> thenThrow(Throwable... throwables) {
    if (throwables == null) {
        thenThrow((Throwable) null);
    }
    OngoingStubbing<T> stubbing = null;
    for (Throwable t: throwables) {
        if (stubbing == null) {
            stubbing = thenThrow(t);                    
        } else {
            stubbing = stubbing.thenThrow(t);
        }
    }
    return stubbing;
}
 
Example 5
Source File: BaseStubbing.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public OngoingStubbing<T> thenThrow(Throwable... throwables) {
    if (throwables == null) {
        thenThrow((Throwable) null);
    }
    OngoingStubbing<T> stubbing = null;
    for (Throwable t: throwables) {
        if (stubbing == null) {
            stubbing = thenThrow(t);                    
        } else {
            stubbing = stubbing.thenThrow(t);
        }
    }
    return stubbing;
}
 
Example 6
Source File: BaseStubbing.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public OngoingStubbing<T> thenThrow(Class<? extends Throwable>... throwableClasses) {
    if (throwableClasses == null) {
        thenThrow((Throwable) null);
    }
    OngoingStubbing<T> stubbing = null;
    for (Class<? extends Throwable> t: throwableClasses) {
        if (stubbing == null) {
            stubbing = thenThrow(t);
        } else {
            stubbing = stubbing.thenThrow(t);
        }
    }
    return stubbing;
}
 
Example 7
Source File: HBaseTableImplTest.java    From pentaho-hadoop-shims with Apache License 2.0 5 votes vote down vote up
private void testIOEGettingHandle( IOERunnable runnable, OngoingStubbing<HBaseConnectionHandle> ongoingStubbing )
  throws IOException {
  IOException ioException = new IOException();
  ongoingStubbing.thenThrow( ioException );
  try {
    runnable.run();
  } catch ( IOException e ) {
    assertEquals( ioException, e.getCause() );
    throw e;
  }
}
 
Example 8
Source File: HBaseTableImplTest.java    From pentaho-hadoop-shims with Apache License 2.0 5 votes vote down vote up
private void testEStillClosesHandle( String name, OngoingStubbing<?> ongoingStubbing, IOERunnable runnable )
  throws IOException {
  Exception exception = new Exception();
  ongoingStubbing.thenThrow( exception );
  when( hBaseConnectionPool.getConnectionHandle( name ) ).thenReturn( hBaseConnectionHandle );
  new EStillClosesRunnable( runnable, exception ).run();
}
 
Example 9
Source File: BulkProcessorTest.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
private RestClient mockClientResponses(RestClient.BulkActionResponse... responses) {
    RestClient mockClient = Mockito.mock(RestClient.class);

    OngoingStubbing<RestClient.BulkActionResponse> stubb = Mockito.when(
            mockClient.bulk(Mockito.eq(resource), Mockito.any(TrackingBytesArray.class))
    );

    for (RestClient.BulkActionResponse response : responses) {
        stubb = stubb.thenReturn(response);
    }

    stubb.thenThrow(new AssertionError("Exhausted all given test responses."));

    return mockClient;
}
 
Example 10
Source File: AMQPObservableQueueTest.java    From conductor with Apache License 2.0 4 votes vote down vote up
Channel mockChannelForExchange(Channel channel, boolean isWorking, boolean exists, String queueName, String name,
		String type, String routingKey, List<GetResponse> queue) throws IOException {
	// exchangeDeclarePassive
	final AMQImpl.Exchange.DeclareOk exchangeDeclareOK = new AMQImpl.Exchange.DeclareOk();
	if (exists) {
		Mockito.when(channel.exchangeDeclarePassive(eq(name))).thenReturn(exchangeDeclareOK);
	} else {
		Mockito.when(channel.exchangeDeclarePassive(eq(name)))
				.thenThrow(new IOException("Exchange " + name + " exists"));
	}
	// exchangeDeclare
	OngoingStubbing<AMQP.Exchange.DeclareOk> declareOkOngoingStubbing = Mockito.when(channel
			.exchangeDeclare(eq(name), eq(type), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap()))
			.thenReturn(exchangeDeclareOK);
	if (!isWorking) {
		declareOkOngoingStubbing.thenThrow(new IOException("Cannot declare exchange " + name + " of type " + type),
				new RuntimeException("Not working"));
	}
	// queueDeclarePassive
	final AMQImpl.Queue.DeclareOk queueDeclareOK = new AMQImpl.Queue.DeclareOk(queueName, queue.size(), 1);
	if (exists) {
		Mockito.when(channel.queueDeclarePassive(eq(queueName))).thenReturn(queueDeclareOK);
	} else {
		Mockito.when(channel.queueDeclarePassive(eq(queueName)))
				.thenThrow(new IOException("Queue " + queueName + " exists"));
	}
	// queueDeclare
	Mockito.when(channel.queueDeclare(eq(queueName), Mockito.anyBoolean(), Mockito.anyBoolean(),
			Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(queueDeclareOK);
	// queueBind
	Mockito.when(channel.queueBind(eq(queueName), eq(name), eq(routingKey))).thenReturn(new AMQImpl.Queue.BindOk());
	// messageCount
	Mockito.when(channel.messageCount(eq(name))).thenReturn(1l * queue.size());
	// basicGet
	
	OngoingStubbing<String> getResponseOngoingStubbing = Mockito
			.when(channel.basicConsume(eq(queueName), Mockito.anyBoolean(), (Consumer) Mockito.any(Consumer.class))).thenAnswer(new ReturnsElementsOf(queue));

	if (!isWorking) {
		getResponseOngoingStubbing.thenThrow(new IOException("Not working"), new RuntimeException("Not working"));
	}
	// basicPublish
	if (isWorking) {
		Mockito.doNothing().when(channel).basicPublish(eq(name), eq(routingKey),
				Mockito.any(AMQP.BasicProperties.class), Mockito.any(byte[].class));
	} else {
		Mockito.doThrow(new IOException("Not working")).when(channel).basicPublish(eq(name), eq(routingKey),
				Mockito.any(AMQP.BasicProperties.class), Mockito.any(byte[].class));
	}
	return channel;
}
 
Example 11
Source File: HBaseTableImplTest.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
private void testEStillClosesHandle( OngoingStubbing<?> ongoingStubbing, IOERunnable runnable ) throws IOException {
  Exception exception = new Exception();
  ongoingStubbing.thenThrow( exception );
  when( hBaseConnectionPool.getConnectionHandle() ).thenReturn( hBaseConnectionHandle );
  new EStillClosesRunnable( runnable, exception ).run();
}