1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.concurrentmockito; 6 7 import org.junit.Test; 8 import org.junit.runner.JUnitCore; 9 import org.junit.runner.Result; 10 import org.junit.runner.notification.Failure; 11 import org.mockito.MockitoTest; 12 import org.mockito.internal.exceptions.ReporterTest; 13 import org.mockito.exceptions.base.MockitoAssertionErrorTest; 14 import org.mockito.exceptions.base.MockitoExceptionTest; 15 import org.mockito.internal.AllInvocationsFinderTest; 16 import org.mockito.internal.InvalidStateDetectionTest; 17 import org.mockito.internal.creation.bytebuddy.TypeCachingMockBytecodeGeneratorTest; 18 import org.mockito.internal.handler.MockHandlerImplTest; 19 import org.mockito.internal.invocation.InvocationImplTest; 20 import org.mockito.internal.invocation.InvocationMatcherTest; 21 import org.mockito.internal.invocation.InvocationsFinderTest; 22 import org.mockito.internal.matchers.ComparableMatchersTest; 23 import org.mockito.internal.matchers.EqualsTest; 24 import org.mockito.internal.matchers.MatchersToStringTest; 25 import org.mockito.internal.progress.MockingProgressImplTest; 26 import org.mockito.internal.progress.TimesTest; 27 import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValuesTest; 28 import org.mockito.internal.stubbing.defaultanswers.ReturnsGenericDeepStubsTest; 29 import org.mockito.internal.util.MockUtilTest; 30 import org.mockito.internal.util.collections.ListUtilTest; 31 import org.mockito.internal.verification.DefaultRegisteredInvocationsTest; 32 import org.mockito.internal.verification.checkers.MissingInvocationCheckerTest; 33 import org.mockito.internal.verification.checkers.MissingInvocationInOrderCheckerTest; 34 import org.mockito.internal.verification.checkers.NumberOfInvocationsCheckerTest; 35 import org.mockito.internal.verification.checkers.NumberOfInvocationsInOrderCheckerTest; 36 import org.mockitousage.basicapi.ReplacingObjectMethodsTest; 37 import org.mockitousage.basicapi.ResetTest; 38 import org.mockitousage.basicapi.UsingVarargsTest; 39 import org.mockitousage.examples.use.ExampleTest; 40 import org.mockitousage.matchers.CustomMatchersTest; 41 import org.mockitousage.matchers.InvalidUseOfMatchersTest; 42 import org.mockitousage.matchers.MatchersTest; 43 import org.mockitousage.matchers.VerificationAndStubbingUsingMatchersTest; 44 import org.mockitousage.misuse.InvalidUsageTest; 45 import org.mockitousage.puzzlers.BridgeMethodPuzzleTest; 46 import org.mockitousage.puzzlers.OverloadingPuzzleTest; 47 import org.mockitousage.stacktrace.ClickableStackTracesTest; 48 import org.mockitousage.stacktrace.PointingStackTraceToActualInvocationTest; 49 import org.mockitousage.stacktrace.StackTraceFilteringTest; 50 import org.mockitousage.stubbing.BasicStubbingTest; 51 import org.mockitousage.stubbing.ReturningDefaultValuesTest; 52 import org.mockitousage.stubbing.StubbingWithThrowablesTest; 53 import org.mockitousage.verification.*; 54 import org.mockitoutil.TestBase; 55 56 import java.util.*; 57 58 import static junit.framework.TestCase.assertEquals; 59 import static junit.framework.TestCase.assertFalse; 60 61 public class ThreadsRunAllTestsHalfManualTest extends TestBase { 62 63 private static class AllTestsRunner extends Thread { 64 65 private Set<Class<?>> failed = new HashSet<Class<?>>(); 66 67 public void run() { 68 Result result = JUnitCore.runClasses( 69 EqualsTest.class, 70 ListUtilTest.class, 71 MockingProgressImplTest.class, 72 TimesTest.class, 73 MockHandlerImplTest.class, 74 AllInvocationsFinderTest.class, 75 ReturnsEmptyValuesTest.class, 76 NumberOfInvocationsCheckerTest.class, 77 DefaultRegisteredInvocationsTest.class, 78 MissingInvocationCheckerTest.class, 79 NumberOfInvocationsInOrderCheckerTest.class, 80 MissingInvocationInOrderCheckerTest.class, 81 TypeCachingMockBytecodeGeneratorTest.class, 82 InvocationMatcherTest.class, 83 InvocationsFinderTest.class, 84 InvocationImplTest.class, 85 MockitoTest.class, 86 MockUtilTest.class, 87 ReporterTest.class, 88 MockitoAssertionErrorTest.class, 89 MockitoExceptionTest.class, 90 StackTraceFilteringTest.class, 91 BridgeMethodPuzzleTest.class, 92 OverloadingPuzzleTest.class, 93 InvalidUsageTest.class, 94 UsingVarargsTest.class, 95 CustomMatchersTest.class, 96 ComparableMatchersTest.class, 97 InvalidUseOfMatchersTest.class, 98 MatchersTest.class, 99 MatchersToStringTest.class, 100 VerificationAndStubbingUsingMatchersTest.class, 101 BasicStubbingTest.class, 102 ReturningDefaultValuesTest.class, 103 StubbingWithThrowablesTest.class, 104 AtMostXVerificationTest.class, 105 BasicVerificationTest.class, 106 ExactNumberOfTimesVerificationTest.class, 107 VerificationInOrderTest.class, 108 NoMoreInteractionsVerificationTest.class, 109 SelectedMocksInOrderVerificationTest.class, 110 VerificationOnMultipleMocksUsingMatchersTest.class, 111 VerificationUsingMatchersTest.class, 112 RelaxedVerificationInOrderTest.class, 113 DescriptiveMessagesWhenVerificationFailsTest.class, 114 DescriptiveMessagesWhenTimesXVerificationFailsTest.class, 115 BasicVerificationInOrderTest.class, 116 VerificationInOrderMixedWithOrdiraryVerificationTest.class, 117 DescriptiveMessagesOnVerificationInOrderErrorsTest.class, 118 InvalidStateDetectionTest.class, 119 ReplacingObjectMethodsTest.class, 120 ClickableStackTracesTest.class, 121 ExampleTest.class, 122 PointingStackTraceToActualInvocationTest.class, 123 VerificationInOrderFromMultipleThreadsTest.class, 124 ResetTest.class, 125 ReturnsGenericDeepStubsTest.class 126 ); 127 128 if (!result.wasSuccessful()) { 129 System.err.println("Thread[" + Thread.currentThread().getId() + "]: error!"); 130 List<Failure> failures = result.getFailures(); 131 System.err.println(failures.size()); 132 for (Failure failure : failures) { 133 System.err.println(failure.getTrace()); 134 failed.add(failure.getDescription().getTestClass()); 135 } 136 } 137 } 138 139 public Set<Class<?>> getFailed() { 140 return failed; 141 } 142 } 143 144 @Test 145 public void shouldRunInMultipleThreads() throws Exception { 146 //this test ALWAYS fails if there is a single failing unit 147 assertEquals("Run in multiple thread failed for tests", Collections.emptySet(), runInMultipleThreads(3)); 148 } 149 150 public static Set<Class<?>> runInMultipleThreads(int numberOfThreads) throws Exception { 151 List<AllTestsRunner> threads = new LinkedList<AllTestsRunner>(); 152 for (int i = 1; i <= numberOfThreads; i++) { 153 threads.add(new AllTestsRunner()); 154 } 155 156 for (Thread t : threads) { 157 t.start(); 158 } 159 160 Set<Class<?>> failed = new HashSet<Class<?>>(); 161 for (AllTestsRunner t : threads) { 162 t.join(); 163 failed.addAll(t.getFailed()); 164 } 165 166 return failed; 167 } 168 169 public static void main(String[] args) throws Exception { 170 int numberOfThreads = 20; 171 long before = System.currentTimeMillis(); 172 Set<Class<?>> failed = runInMultipleThreads(numberOfThreads); 173 long after = System.currentTimeMillis(); 174 long executionTime = (after-before)/1000; 175 System.out.println("Finished tests in " + numberOfThreads + " threads in " + executionTime + " seconds. (" + failed.size() + " tests failed)"); 176 } 177 } 178