Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created October 5, 2016 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viacheslav77/0133c0707f4f0bc5eb574763ed3aa881 to your computer and use it in GitHub Desktop.
Save Viacheslav77/0133c0707f4f0bc5eb574763ed3aa881 to your computer and use it in GitHub Desktop.
Создать аннотацию Test, которая принимает параметры для процедуры тестирования и передает их в метод, помеченный такой аннотацией.
SomeClass: true
false
package tester1;
/*Создать аннотацию Test, которая принимает параметры для процедуры тестирования и передает их в
метод, помеченный такой аннотацией.*/
public class Main {
public static void main (String[] args){
System.out.println (Tester.testSum(SomeClass.class));
}
}
package tester1;
public class SomeClass {
public static int sum(int a, int b){
return a+b;
}
@Test (a=2,b=3)
public static boolean test (int a, int b){
boolean res = sum (a,b) == a+b;
System.out.println ("SomeClass: " + res);
return res;
}
}
package tester1;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value=ElementType.METHOD)
@Retention(value=RetentionPolicy.RUNTIME)
public @interface Test {
int a ();
int b ();
}
package tester1;
import java.lang.reflect.Method;
public class Tester {
public static boolean testSum (Class<?>...cls) {
try {
for (Class<?> ls: cls ){
Method[] methods = ls.getDeclaredMethods();
for(Method method : methods) {
if (method.isAnnotationPresent(Test.class)){
Test t = method.getAnnotation(Test.class);
Boolean b = (Boolean) method.invoke(cls, t.a(),t.b());
if ( ! b );
return false;
}
}
}
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment