protected TextMessage createTextMessage(MessageCreator creator) throws JMSException { Session mock = mock(Session.class); given(mock.createTextMessage(BDDMockito.any())).willAnswer( (Answer<TextMessage>) invocation -> new StubTextMessage((String) invocation.getArguments()[0])); javax.jms.Message message = creator.createMessage(mock); verify(mock).createTextMessage(BDDMockito.any()); return TextMessage.class.cast(message); }
private DataSource createMockDataSource() { try { final DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willAnswer(new Answer<Connection>() { @Override public Connection answer(final InvocationOnMock invocation) throws Throwable { return jdbcRule.getConnectionSource().getConnection(); } }); return dataSource; } catch (final SQLException e) { Throwables.rethrow(e); throw new InternalError("unreachable"); } }
private DataSource createMockDataSource() { try { final DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willAnswer(new Answer<Connection>() { @Override public Connection answer(final InvocationOnMock invocation) throws Throwable { return jdbcRule.getConnectionSource().getConnection(); } }); return dataSource; } catch (final SQLException e) { Throwables.rethrow(e); throw new InternalError("unreachable"); } }
@Test void stubbingWithCustomAnswerShouldBeLongerEvenAsLambda() { //given given(ts.findNumberOfShipsInRangeByCriteria(any())).willAnswer(invocation -> { ShipSearchCriteria criteria = (ShipSearchCriteria) invocation.getArguments()[0]; return criteria.getMinimumRange() > 1000 ? 4 : 0; }); //expect assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(1500, 2))).isEqualTo(4); //expect assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(700, 2))).isEqualTo(0); } }
ConsumerRecords<Integer, String> emptyRecords = new ConsumerRecords<>(Collections.emptyMap()); AtomicInteger which = new AtomicInteger(); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); int recordsToUse = which.incrementAndGet();
ConsumerRecords<Integer, String> emptyRecords = new ConsumerRecords<>(Collections.emptyMap()); AtomicBoolean first = new AtomicBoolean(true); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); return first.getAndSet(false) ? consumerRecords : emptyRecords;
new ConsumerRecord<>("foo", 0, 1L, 1, "bar"))); ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); return consumerRecords;
ConsumerRecords<Integer, String> emptyRecords = new ConsumerRecords<>(Collections.emptyMap()); AtomicBoolean first = new AtomicBoolean(true); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); return first.getAndSet(false) ? consumerRecords : emptyRecords;
Lock firstLock = mock(Lock.class); given(firstRegistry.obtain(anyString())).willReturn(firstLock); given(firstLock.tryLock(anyLong(), any(TimeUnit.class))).willAnswer(i -> firstLocked.get()); Lock secondLock = mock(Lock.class); given(secondRegistry.obtain(anyString())).willReturn(secondLock); given(secondLock.tryLock(anyLong(), any(TimeUnit.class))).willAnswer(i -> !firstLocked.get());
ConsumerRecords<Integer, String> emptyRecords = new ConsumerRecords<>(Collections.emptyMap()); final CountDownLatch latch = new CountDownLatch(1); given(consumer.poll(any(Duration.class))).willAnswer(i -> { latch.countDown(); Thread.sleep(50);
new ConsumerRecord<>("foo", 0, 1L, 1, "bar"))); ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); return consumerRecords;
given(cf.createConsumer(anyString(), anyString(), anyString())).willReturn(consumer); given(consumer.poll(any(Duration.class))) .willAnswer(new Answer<ConsumerRecords<Integer, String>>() {
new ConsumerRecord<>("foo", 0, 1L, 1, "bar"))); ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); return consumerRecords;
new ConsumerRecord<>("foo", 0, 1L, 1, "bar"))); ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(50); return consumerRecords;
ConsumerRecords records = new ConsumerRecords(Collections.emptyMap()); CountDownLatch latch = new CountDownLatch(20); given(consumer.poll(any(Duration.class))).willAnswer(i -> { Thread.sleep(100); latch.countDown();
new ConsumerRecord<>("foo", 0, 1L, 1, "bar"))); final CountDownLatch deadLatch = new CountDownLatch(1); given(consumer.poll(any(Duration.class))).willAnswer(i -> { deadLatch.await(10, TimeUnit.SECONDS); throw new WakeupException();
@Before public void setUp() { given(provider.getConnection()).willReturn(connection); given(connection.createObject()).willAnswer(new Answer<DefaultNoSqlObject>() { @Override public DefaultNoSqlObject answer(final InvocationOnMock invocation) throws Throwable { return new DefaultNoSqlObject(); } }); given(connection.createList(anyInt())).willAnswer(new Answer<DefaultNoSqlObject[]>() { @Override public DefaultNoSqlObject[] answer(final InvocationOnMock invocation) throws Throwable { return new DefaultNoSqlObject[invocation.<Integer>getArgument(0)]; } }); }
@Test public void testByteArrayConversion() throws JMSException { Session session = mock(Session.class); BytesMessage message = mock(BytesMessage.class); byte[] content = "test".getBytes(); final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content); given(session.createBytesMessage()).willReturn(message); given(message.getBodyLength()).willReturn((long) content.length); given(message.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]); } }); SimpleMessageConverter converter = new SimpleMessageConverter(); Message msg = converter.toMessage(content, session); assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length); verify(message).writeBytes(content); }
@Test public void fromBytesMessage() throws Exception { BytesMessage bytesMessageMock = mock(BytesMessage.class); Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); byte[] bytes = "{\"foo\":\"bar\"}".getBytes(); final ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName()); given(bytesMessageMock.propertyExists("__encoding__")).willReturn(false); given(bytesMessageMock.getBodyLength()).willReturn(new Long(bytes.length)); given(bytesMessageMock.readBytes(any(byte[].class))).willAnswer( new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { return byteStream.read((byte[]) invocation.getArguments()[0]); } }); Object result = converter.fromMessage(bytesMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void testWithMessageContentsDelegateForBytesMessage() throws Exception { BytesMessage bytesMessage = mock(BytesMessage.class); // BytesMessage contents must be unwrapped... given(bytesMessage.getBodyLength()).willReturn(new Long(TEXT.getBytes().length)); given(bytesMessage.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { byte[] bytes = (byte[]) invocation.getArguments()[0]; ByteArrayInputStream inputStream = new ByteArrayInputStream(TEXT.getBytes()); return inputStream.read(bytes); } }); MessageContentsDelegate delegate = mock(MessageContentsDelegate.class); MessageListenerAdapter adapter = new MessageListenerAdapter(delegate); adapter.onMessage(bytesMessage); verify(delegate).handleMessage(TEXT.getBytes()); }