001/** 002 * The MIT License (MIT) 003 * 004 * Copyright (c) 2017 tools4j.org (Marco Terzer) 005 * 006 * Permission is hereby granted, free of charge, to any person obtaining a copy 007 * of this software and associated documentation files (the "Software"), to deal 008 * in the Software without restriction, including without limitation the rights 009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010 * copies of the Software, and to permit persons to whom the Software is 011 * furnished to do so, subject to the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be included in all 014 * copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 022 * SOFTWARE. 023 */ 024package org.tools4j.spockito; 025 026import org.junit.runner.Description; 027import org.junit.runner.manipulation.Filter; 028import org.junit.runner.manipulation.NoTestsRemainException; 029import org.junit.runner.notification.RunNotifier; 030import org.junit.runners.BlockJUnit4ClassRunner; 031import org.junit.runners.model.FrameworkMethod; 032import org.junit.runners.model.InitializationError; 033import org.junit.runners.model.Statement; 034 035import java.lang.annotation.Annotation; 036import java.util.List; 037 038/** 039 * Common base for spockito test runners. 040 */ 041abstract public class AbstractSpockitoTestRunner extends BlockJUnit4ClassRunner { 042 043 public AbstractSpockitoTestRunner(final Class<?> clazz) throws InitializationError { 044 super(clazz); 045 } 046 047 @Override 048 protected Statement classBlock(RunNotifier notifier) { 049 return childrenInvoker(notifier); 050 } 051 052 @Override 053 protected Annotation[] getRunnerAnnotations() { 054 return new Annotation[0]; 055 } 056 057 @Override 058 public void filter(final Filter filter) throws NoTestsRemainException { 059 if (filter instanceof MethodLevelFilter) { 060 //Spockito does the necessary filtering with MethodLevelFilter 061 super.filter(Filter.ALL); 062 } else { 063 super.filter(filter); 064 } 065 } 066 067 @Override 068 protected Description describeChild(final FrameworkMethod method) { 069 final Spockito.Name name = Spockito.nameAnnotationOrNull(method.getMethod()); 070 if (name != null && name.shortFormat()) { 071 return Description.createSuiteDescription(testName(method), method.getAnnotations()); 072 } else { 073 return super.describeChild(method); 074 } 075 } 076 077 @Override 078 protected void collectInitializationErrors(final List<Throwable> errors) { 079 //don't do here, do validation in our own constructor 080 } 081 082}