private ConnectionFactory createFailingContainerFactory() { try { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).will(invocation -> { throw new JMSException("Test exception"); }); return connectionFactory; } catch (JMSException ex) { throw new IllegalStateException(ex); // never happen } }
private ConnectionFactory createRecoverableContainerFactory(final int failingAttempts) { try { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).will(new Answer<Object>() { int currentAttempts = 0; @Override public Object answer(InvocationOnMock invocation) throws Throwable { currentAttempts++; if (currentAttempts <= failingAttempts) { throw new JMSException("Test exception (attempt " + currentAttempts + ")"); } else { return mock(Connection.class); } } }); return connectionFactory; } catch (JMSException ex) { throw new IllegalStateException(ex); // never happen } }
BDDMockito.given(helloWorldService.returnHelloWorld()).will(invocation -> { if (countDownLatch.await(10, TimeUnit.SECONDS)) { return "Hello world";
final Map<String, String> initParameters = new HashMap<>(); given(this.servletContext.setInitParameter(anyString(), anyString())) .will((invocation) -> { initParameters.put(invocation.getArgument(0), invocation.getArgument(1));
}; given(dexFileLoader.loadDex(anyString(), anyString())) .will(answer((src, out) -> { dalvik.system.DexFile file = mock(dalvik.system.DexFile.class, withSettings().stubOnly());
@Test public void should_generate_non_final_classes() throws IOException { ImmutableClassDef def = new ImmutableClassDef("Lfoo/Bar;", AccessFlags.FINAL.getValue(), "Ljava/lang/Object;", null, null, null, null, null); byte[] bytes = DexPoolUtils.toBytecode(new ImmutableDexFile(Opcodes.getDefault(), Collections.singleton(def))); given(dexFileLoader.loadDex(srcPathCaptor.capture(), anyString())) .will(answer((String src, String out) -> { assertTrue(src.endsWith("tmp.dex")); assertTrue(out.endsWith(".dex")); DexFile file; try (InputStream in = new FileInputStream(src)) { file = new DexBackedDexFile(null, ByteStreams.toByteArray(in)); } Set<? extends ClassDef> classes = file.getClasses(); assertSame(1, classes.size()); assertFalse(AccessFlags.FINAL.isSet(classes.iterator().next().getAccessFlags())); return dexFile; })); DexFile file = new DexBackedDexFile(null, bytes); ClassTransformer transformer = new ClassTransformer(file.getOpcodes(), file.getClasses(), folder.newFolder(), dexFileLoader); assertSame(dexFile, transformer.call()); assertTrue(transformer.getClasses().isEmpty()); assertFalse(new File(srcPathCaptor.getValue()).exists()); }
@Bean(name = "twitterTemplate") public TwitterTemplate twitterTemplate() { TwitterTemplate twitterTemplate = mock(TwitterTemplate.class); SearchOperations so = mock(SearchOperations.class); Tweet tweet3 = mock(Tweet.class); given(tweet3.getId()).willReturn(3L); given(tweet3.getCreatedAt()).willReturn(new GregorianCalendar(2013, 2, 20).getTime()); given(tweet3.toString()).will(invocation -> "Mock for Tweet: " + tweet3.getId()); Tweet tweet1 = mock(Tweet.class); given(tweet1.getId()).willReturn(1L); given(tweet1.getCreatedAt()).willReturn(new GregorianCalendar(2013, 0, 20).getTime()); given(tweet1.toString()).will(invocation -> "Mock for Tweet: " + tweet1.getId()); final Tweet tweet2 = mock(Tweet.class); given(tweet2.getId()).willReturn(2L); given(tweet2.getCreatedAt()).willReturn(new GregorianCalendar(2013, 1, 20).getTime()); given(tweet2.toString()).will(invocation -> "Mock for Tweet: " + tweet2.getId()); final List<Tweet> tweets = new ArrayList<Tweet>(); tweets.add(tweet3); tweets.add(tweet1); tweets.add(tweet2); final SearchResults results = new SearchResults(tweets, new SearchMetadata(111, 111)); given(twitterTemplate.searchOperations()).willReturn(so); given(twitterTemplate.searchOperations().search(any(SearchParameters.class))).willReturn(results); given(twitterTemplate.isAuthorized()).willReturn(true); final UserOperations userOperations = mock(UserOperations.class); given(twitterTemplate.userOperations()).willReturn(userOperations); given(userOperations.getProfileId()).willReturn(74L); return twitterTemplate; }
BDDMockito.given(helloWorldService.returnHelloWorld()).will(invocation -> { if (countDownLatch.await(10, TimeUnit.SECONDS)) { return "Hello world";