Skip to content

Instantly share code, notes, and snippets.

@suisho
Created December 15, 2012 14:20
Show Gist options
  • Save suisho/4295497 to your computer and use it in GitHub Desktop.
Save suisho/4295497 to your computer and use it in GitHub Desktop.
get annotation on junit setup
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface DependsOnPlatform {
public String value();
}
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
public class SomeClassTest {
@Rule
public TestName name = new TestName();
@Before
public void setUp() {
try {
Method method = this.getClass().getMethod(this.name.getMethodName(), new Class[0]);
DependsOnPlatform annotation = method.getAnnotation(DependsOnPlatform.class);
switch(annotation.value()){
case "windows":
//window用のsetup
System.out.println("Windows setup");
break;
case "linux":
//linux用のsetup
System.out.println("Linux setup");
break;
}
} catch (NoSuchMethodException | SecurityException noSuchMethodException) {
}
}
@Test
@DependsOnPlatform(value="windows")
public void windowsTest() {
//windowsでしか動かないテスト
}
@Test
@DependsOnPlatform(value="linux")
public void linuxTest() {
//linuxでしか動かないテスト
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment