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