• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.runners;
6 
7 import org.mockito.exceptions.base.MockitoException;
8 import org.mockito.internal.runners.util.RunnerProvider;
9 import org.mockito.internal.runners.util.TestMethodsFinder;
10 
11 import java.lang.reflect.InvocationTargetException;
12 
13 public class RunnerFactory {
14 
15     private final RunnerProvider runnerProvider;
16 
RunnerFactory(RunnerProvider runnerProvider)17     RunnerFactory(RunnerProvider runnerProvider) {
18         this.runnerProvider = runnerProvider;
19     }
20 
RunnerFactory()21     public RunnerFactory() {
22         this(new RunnerProvider());
23     }
24 
create(Class<?> klass)25     public RunnerImpl create(Class<?> klass) throws InvocationTargetException {
26         try {
27             if (runnerProvider.isJUnit45OrHigherAvailable()) {
28                 return runnerProvider.newInstance("org.mockito.internal.runners.JUnit45AndHigherRunnerImpl", klass);
29             } else {
30                 return runnerProvider.newInstance("org.mockito.internal.runners.JUnit44RunnerImpl", klass);
31             }
32         } catch (InvocationTargetException e) {
33             if (!new TestMethodsFinder().hasTestMethods(klass)) {
34                 throw new MockitoException(
35                     "\n" +
36                     "\n" +
37                     "No tests found in " + klass.getSimpleName() + "\n" +
38                     "Haven't you forgot @Test annotation?\n"
39                     , e);
40             }
41             throw e;
42         } catch (Throwable t) {
43             throw new MockitoException(
44                     "\n" +
45                     "\n" +
46                     "MockitoRunner can only be used with JUnit 4.4 or higher.\n" +
47                     "You can upgrade your JUnit version or write your own Runner (please consider contributing your runner to the Mockito community).\n" +
48                     "Bear in mind that you can still enjoy all features of the framework without using runners (they are completely optional).\n" +
49                     "If you get this error despite using JUnit 4.4 or higher then please report this error to the mockito mailing list.\n"
50                     , t);
51         }
52     }
53 }