2017-12-13 2 views
0

테스트 메소드에 주석 테스트 데이터 유형이있는 testNG에서 일부 테스트를 실행하고 있습니다. 일반적인 메소드를 사용하여 읽고 있습니다. 테스트 메소드의 주석 값을 기반으로 테스트 데이터. 여기서 문제는 테스트 데이터를 읽을 다른 클래스의 여러/여러 테스트 메소드가 있으므로 주석을 읽는 클래스 또는 메소드 이름을 지정할 수 없다는 것입니다. 그래서 어떤 테스트 메소드가 테스트 데이터를 찾고 있는지에 따라이 공통 메소드에서 주석을 동적으로 읽을 수있는 방법을 찾고 있습니다.주석 값을 얻기 위해 클래스/메소드 이름을 하드 코딩하지 않고 주석 값을 동적으로 가져옵니다.

더 명확하게 보려면 아래 코드 스 니펫을 참조하십시오.

public class MyClass1 { 

    @Test 
    @testDataParam(testDataType="excel") 
    public void test1() { 

     DataTable dataTable = new DataTable(); 
     dataTable.getValue(); 
     //Some test that reads from specified test data type 
    } 

} 

public class MyClass2 { 

    @Test 
    @testDataParam(testDataType="json") 
    public void test2() { 

     DataTable dataTable = new DataTable(); 
     dataTable.getValue(); 
     //Some test that reads from specified test data type 
    } 

} 

public class GetTestData { 

    @Target(ElementType.METHOD) 
    @Retention(RetentionPolicy.RUNTIME) 
    public @interface testDataParam { 
     String testDataType(); 
    } 

} 

public class DataTable { 

    public void setDataTable() { 

     // get annotation from test methods, test methods will be different for 
     // different test, so i can not mention specific class or method here to read 
     // annotation. 

     // if test data type is excel, read data from excel 
     // if test data type is json, read data from json 
    } 

    public String getValue() { 

     // return value from specified data type excel/json 
     return ""; 
    } 
} 

답변

2

주석이 달린 메소드에는 매개 변수가 없기 때문에 스택 추적을 요청하고 메소드를 검색하여 주석을 찾을 수 있습니다. 당신이 test1() 방법을 실행하면,이 출력을 얻을

import java.lang.annotation.*; 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface TestData { 
    String value(); 
} 
public class MyClass1 { 
    @TestData("Foo") 
    public void test1() { 
     Util.doSomething(); 
    } 
} 
public class Util { 
    public static void doSomething() { 
     for (StackTraceElement stackTrace : Thread.currentThread().getStackTrace()) { 
      try { 
       TestData annotation = Class.forName(stackTrace.getClassName()) 
              .getDeclaredMethod(stackTrace.getMethodName()) 
              .getAnnotation(TestData.class); 
       if (annotation != null) 
        System.out.println("Test data is: " + annotation.value()); 
      } catch (Exception e) { 
       // Ignore 
      } 
     } 
    } 
} 

:

Test data is: Foo 
0

내가이 문제를 해결했다 여기

은 예입니다 코딩 문제가있는 probl 내가 그랬어. 나는 해결책의 절반 밖에 가지고 있지 않습니다.

클래스를 가져 오려면 패키지에 선언 된 모든 클래스와 하위 클래스를 가져 오는 Fast Classpath Scanner을 사용했습니다. 그럼 당신은 클래스


Class 유형은 당신이 그 안에 선언 된 모든 방법, 즉 [class object].getDeclaredMethods()을 얻을 수있는 방법이있다의 목록을 얻을 수 result.classNamesToClassRefs(result.getNamesOfAllClasses())을 수행합니다. Class를 얻으려면, 당신이해야 할 하나 MyClass1.class 또는

new MyClass1().getClass()Method 배열에서 원하는 방법, 당신은 method.getAnnotation(Test.class)를 호출하고 null의 경우 확인할 수 있습니다 getDeclaredMethods()에서 반환 얻을 수 있습니다. 그렇지 않은 경우 다음을 사용할 수 있습니다.

testDataParam annotation = method.getAnnotation(testDataParam.class); 
Sting dataType = annotation.testDataType(); 

주석에서 데이터 유형을 추출하려면 다음을 사용할 수 있습니다. 전체에서

:

//Put the classes into a Class<?> array 
FastClasspathScanner scanner = new FastClasspathScanner("YOUR PACKAGE HERE"); 
ScanResult result = scanner.scan(); 

for (Class<?> cls : result.classNamesToClassRefs(result.getNamesOfAllClasses())) { 
    for (Method method : cls.getDeclaredMethods()) { 
     if (method.getAnnotation(Test.class) != null) { 
      testDataParam annotation = method.getAnnotation(testDataParam.class); 
      Sting dataType = annotation.testDataType(); 
      //Do what you want with this 
     } 
    } 
} 
관련 문제