Skip to content

Instantly share code, notes, and snippets.

@barend
Created March 10, 2011 17:26
Show Gist options
  • Save barend/864506 to your computer and use it in GitHub Desktop.
Save barend/864506 to your computer and use it in GitHub Desktop.
A JUnit Runner that behaves like the default runner on Windows and ignores all tests on other operating systems.
//http://www.apache.org/licenses/LICENSE-2.0
package nl.fnord.junit;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
/**
* A JUnit {@code Runner} that behaves like the default runner on Windows and
* ignores all tests on other operating systems.
* <h3>Usage:</h3>
* <pre>
* &#64;RunWith(OnlyOnWindowsRunner.class)
* public class SomeClassTest {
* &#64;Test
* public void testSomethingWindowsSpecific() {
* // etc.
* }
* }
* </pre>
*/
public class OnlyOnWindowsRunner extends BlockJUnit4ClassRunner {
public OnlyOnWindowsRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
isWindows()
? super.runChild(method, notifier)
: notifier.fireTestIgnored(getDescription);
}
/**
* @return whether the host OS is windows.
*/
private boolean isWindows() {
// This can probably be improved.
return System.getProperty("os.name").indexOf("Windows") > -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment