Skip to content

Instantly share code, notes, and snippets.

@max747
Created May 26, 2011 03:25
Show Gist options
  • Save max747/992486 to your computer and use it in GitHub Desktop.
Save max747/992486 to your computer and use it in GitHub Desktop.
Execute platform-dependent test using JUnit4
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
EachTestNotifier eachNotifier= makeNotifier(method, notifier);
if (method.getAnnotation(Ignore.class) != null) {
runIgnored(eachNotifier);
} else {
runNotIgnored(method, eachNotifier);
}
}
private void runNotIgnored(FrameworkMethod method,
EachTestNotifier eachNotifier) {
eachNotifier.fireTestStarted();
try {
methodBlock(method).evaluate();
} catch (AssumptionViolatedException e) {
eachNotifier.addFailedAssumption(e);
} catch (Throwable e) {
eachNotifier.addFailure(e);
} finally {
eachNotifier.fireTestFinished();
}
}
package extensions;
import org.junit.Ignore;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
public class CustomJUnit4ClassrRunner extends BlockJUnit4ClassRunner {
private static Platform thisMachinesPlatform;
static {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.startsWith("windows")) {
thisMachinesPlatform = Platform.WINDOWS;
} else {
thisMachinesPlatform = Platform.UNIX;
}
}
public CustomJUnit4ClassrRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
EachTestNotifier eachNotifier = makeNotifier(method, notifier);
if (method.getAnnotation(Ignore.class) != null) {
runIgnored(eachNotifier);
} else {
DependsOnPlatform dependsOnPlatform = method.getAnnotation(DependsOnPlatform.class);
if (dependsOnPlatform != null && dependsOnPlatform.platform() != thisMachinesPlatform) {
runIgnored(eachNotifier);
return;
}
runNotIgnored(method, eachNotifier);
}
}
// copy of BlockJUnit4ClassRunner#runNotIgnored
private void runNotIgnored(FrameworkMethod method, EachTestNotifier eachNotifier) {
eachNotifier.fireTestStarted();
try {
methodBlock(method).evaluate();
} catch (AssumptionViolatedException e) {
eachNotifier.addFailedAssumption(e);
} catch (Throwable e) {
eachNotifier.addFailure(e);
} finally {
eachNotifier.fireTestFinished();
}
}
// copy of BlockJUnit4ClassRunner#runIgnored
private void runIgnored(EachTestNotifier eachNotifier) {
eachNotifier.fireTestIgnored();
}
// copy of BlockJUnit4ClassRunner#makeNotifier
private EachTestNotifier makeNotifier(FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
return new EachTestNotifier(notifier, description);
}
}
package yourdomain.yourapp;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.seasar.util.misc.Pair;
import extensions.CustomJUnit4ClassrRunner;
import extensions.DependsOnPlatform;
import extensions.Platform;
@RunWith(CustomJUnit4ClassrRunner.class)
public class ShellCommandTest {
private ShellCommand command = new DefaultShellCommand();
@Test
@DependsOnPlatform(platform = Platform.UNIX)
public void unix_likeな環境でのみ動くテスト() {
Pair<Integer, String> result = command.exec("echo", "foobar");
assertThat(result.getFirst(), is(0));
assertThat(result.getSecond(), is("foobar"));
}
@Test
@DependsOnPlatform(platform = Platform.WINDOWS)
public void windows環境でのみ動くテスト() {
Pair<Integer, String> result = command.exec("echo", "foobar");
assertThat(result.getFirst(), is(0));
assertThat(result.getSecond(), is("foobar\r\n"));
}
}
package extensions;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;
@Retention(RUNTIME)
public @interface DependsOnPlatform {
Platform platform();
}
package extensions;
public enum Platform {
UNIX, WINDOWS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment