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.junit.runner.Description; 8 import org.junit.runner.manipulation.Filter; 9 import org.junit.runner.manipulation.NoTestsRemainException; 10 import org.junit.runner.notification.RunNotifier; 11 import org.junit.runners.BlockJUnit4ClassRunner; 12 import org.junit.runners.model.FrameworkMethod; 13 import org.junit.runners.model.InitializationError; 14 import org.junit.runners.model.Statement; 15 import org.mockito.MockitoAnnotations; 16 import org.mockito.internal.runners.util.FrameworkUsageValidator; 17 18 public class JUnit45AndHigherRunnerImpl implements RunnerImpl { 19 20 private BlockJUnit4ClassRunner runner; 21 JUnit45AndHigherRunnerImpl(Class<?> klass)22 public JUnit45AndHigherRunnerImpl(Class<?> klass) throws InitializationError { 23 runner = new BlockJUnit4ClassRunner(klass) { 24 protected Statement withBefores(FrameworkMethod method, Object target, 25 Statement statement) { 26 // init annotated mocks before tests 27 MockitoAnnotations.initMocks(target); 28 return super.withBefores(method, target, statement); 29 } 30 }; 31 } 32 run(final RunNotifier notifier)33 public void run(final RunNotifier notifier) { 34 // add listener that validates framework usage at the end of each test 35 notifier.addListener(new FrameworkUsageValidator(notifier)); 36 37 runner.run(notifier); 38 } 39 getDescription()40 public Description getDescription() { 41 return runner.getDescription(); 42 } 43 filter(Filter filter)44 public void filter(Filter filter) throws NoTestsRemainException { 45 runner.filter(filter); 46 } 47 }