-
-
Save Viacheslav77/0133c0707f4f0bc5eb574763ed3aa881 to your computer and use it in GitHub Desktop.
Создать аннотацию Test, которая принимает параметры для процедуры тестирования и передает их в метод, помеченный такой аннотацией.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeClass: true | |
false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tester1; | |
/*Создать аннотацию Test, которая принимает параметры для процедуры тестирования и передает их в | |
метод, помеченный такой аннотацией.*/ | |
public class Main { | |
public static void main (String[] args){ | |
System.out.println (Tester.testSum(SomeClass.class)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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