How to use ShouldNotDeadlockAnswerExecutionTest class of org.mockitousage.bugs package

Best Mockito code snippet using org.mockitousage.bugs.ShouldNotDeadlockAnswerExecutionTest

Source:ShouldNotDeadlockAnswerExecutionTest.java Github

copy

Full Screen

...12import org.mockito.Mockito;13import org.mockito.invocation.InvocationOnMock;14import org.mockito.stubbing.Answer;15// see bug 19016public class ShouldNotDeadlockAnswerExecutionTest {17 @Test18 public void failIfMockIsSharedBetweenThreads() throws Exception {19 ShouldNotDeadlockAnswerExecutionTest.Service service = Mockito.mock(ShouldNotDeadlockAnswerExecutionTest.Service.class);20 ExecutorService threads = Executors.newCachedThreadPool();21 AtomicInteger counter = new AtomicInteger(2);22 // registed answer on verySlowMethod23 Mockito.when(service.verySlowMethod()).thenAnswer(new ShouldNotDeadlockAnswerExecutionTest.LockingAnswer(counter));24 // execute verySlowMethod twice in separate threads25 threads.execute(new ShouldNotDeadlockAnswerExecutionTest.ServiceRunner(service));26 threads.execute(new ShouldNotDeadlockAnswerExecutionTest.ServiceRunner(service));27 // waiting for threads to finish28 threads.shutdown();29 if (!(threads.awaitTermination(1000, TimeUnit.MILLISECONDS))) {30 // threads were timed-out31 Assert.fail();32 }33 }34 @Test35 public void successIfEveryThreadHasItsOwnMock() throws Exception {36 ShouldNotDeadlockAnswerExecutionTest.Service service1 = Mockito.mock(ShouldNotDeadlockAnswerExecutionTest.Service.class);37 ShouldNotDeadlockAnswerExecutionTest.Service service2 = Mockito.mock(ShouldNotDeadlockAnswerExecutionTest.Service.class);38 ExecutorService threads = Executors.newCachedThreadPool();39 AtomicInteger counter = new AtomicInteger(2);40 // registed answer on verySlowMethod41 Mockito.when(service1.verySlowMethod()).thenAnswer(new ShouldNotDeadlockAnswerExecutionTest.LockingAnswer(counter));42 Mockito.when(service2.verySlowMethod()).thenAnswer(new ShouldNotDeadlockAnswerExecutionTest.LockingAnswer(counter));43 // execute verySlowMethod twice in separate threads44 threads.execute(new ShouldNotDeadlockAnswerExecutionTest.ServiceRunner(service1));45 threads.execute(new ShouldNotDeadlockAnswerExecutionTest.ServiceRunner(service2));46 // waiting for threads to finish47 threads.shutdown();48 if (!(threads.awaitTermination(500, TimeUnit.MILLISECONDS))) {49 // threads were timed-out50 Assert.fail();51 }52 }53 static class LockingAnswer implements Answer<String> {54 private AtomicInteger counter;55 public LockingAnswer(AtomicInteger counter) {56 this.counter = counter;57 }58 /**59 * Decrement counter and wait until counter has value 060 */61 public String answer(InvocationOnMock invocation) throws Throwable {62 counter.decrementAndGet();63 while ((counter.get()) != 0) {64 Thread.sleep(10);65 } 66 return null;67 }68 }69 static class ServiceRunner implements Runnable {70 private ShouldNotDeadlockAnswerExecutionTest.Service service;71 public ServiceRunner(ShouldNotDeadlockAnswerExecutionTest.Service service) {72 this.service = service;73 }74 public void run() {75 service.verySlowMethod();76 }77 }78 interface Service {79 String verySlowMethod();80 }81}...

Full Screen

Full Screen

ShouldNotDeadlockAnswerExecutionTest

Using AI Code Generation

copy

Full Screen

1package org.mockitousage.bugs;2import org.junit.Test;3import org.mockito.Mockito;4import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;5import org.mockito.exceptions.verification.junit.TooFewActualInvocations;6import org.mockito.exceptions.verification.junit.TooManyActualInvocations;7import org.mockitousage.IMethods;8import org.mockitoutil.TestBase;9import java.util.concurrent.CountDownLatch;10import static org.junit.Assert.fail;11import static org.mockito.Mockito.*;12public class ShouldNotDeadlockAnswerExecutionTest extends TestBase {13 public void should_not_deadlock_when_answer_executes_the_same_method() throws InterruptedException {14 IMethods mock = mock(IMethods.class, new AnswerThatExecutesSameMethod());15 final CountDownLatch latch = new CountDownLatch(1);16 Thread t = new Thread(new Runnable() {17 public void run() {18 mock.simpleMethod(1);19 latch.countDown();20 }21 });22 t.start();23 t.join();24 latch.await();25 verify(mock).simpleMethod(1);26 }27 public void should_not_deadlock_when_answer_executes_another_method() throws InterruptedException {28 IMethods mock = mock(IMethods.class, new AnswerThatExecutesAnotherMethod());29 final CountDownLatch latch = new CountDownLatch(1);30 Thread t = new Thread(new Runnable() {31 public void run() {32 mock.simpleMethod(1);33 latch.countDown();34 }35 });36 t.start();37 t.join();38 latch.await();39 verify(mock).simpleMethod(1);40 }41 public void should_not_deadlock_when_answer_executes_another_mock() throws InterruptedException {42 IMethods mock = mock(IMethods.class, new AnswerThatExecutesAnotherMock());43 final CountDownLatch latch = new CountDownLatch(1);44 Thread t = new Thread(new Runnable() {45 public void run() {46 mock.simpleMethod(1);47 latch.countDown();48 }49 });50 t.start();51 t.join();52 latch.await();53 verify(mock).simpleMethod(1);54 }55 public void should_not_deadlock_when_answer_executes_another_mock_in_another_thread() throws InterruptedException {56 IMethods mock = mock(IMethods.class, new AnswerThatExecutesAnotherMockInAnotherThread());

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful