org.mockito.internal.stubbing.answers.ReturnsElementsOf Java Examples
The following examples show how to use
org.mockito.internal.stubbing.answers.ReturnsElementsOf.
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: AMQPObservableQueueTest.java From conductor with Apache License 2.0 | 5 votes |
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 #2
Source File: AMQPObservableQueueTest.java From conductor with Apache License 2.0 | 4 votes |
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 #3
Source File: AdditionalAnswers.java From astor with GNU General Public License v2.0 | 2 votes |
/** * Returns elements of the collection. Keeps returning the last element forever. * Might be useful on occasion when you have a collection of elements to return. * <p> * <pre class="code"><code class="java"> * //this: * when(mock.foo()).thenReturn(1, 2, 3); * * //is equivalent to: * when(mock.foo()).thenAnswer(new ReturnsElementsOf(Arrays.asList(1, 2, 3))); * </code></pre> * * @param elements The collection of elements to return. * @return the answer * * @since 1.9.5 */ public static <T> Answer<T> returnsElementsOf(Collection<?> elements) { return (Answer<T>) new ReturnsElementsOf(elements); }