Create an instance variable representing other variables with the same name but unique values ​​in different classes (Java)

I am currently trying to integrate automated tests with our API test management tool. Each test (which is contained in one class) has a unique identifier that must be referenced in the API call. I can either declare the test IDs in the tests themselves (preferred), or create a separate class containing all the IDs to reference. The problem I am facing is a good way to represent these ids as a variable when a particular test is run, so I don’t have to repeat the API structure for each unique identifier.

Here is the general setup for the API class / API listener.

  package testPackage;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import com.gurock.testrail.APIClient;
import com.gurock.testrail.APIException;


public class MyTestResultsListener extends TestListenerAdapter {

    public final APIClient client = new APIClient("https://api.url/");
    public final Map data = new HashMap();

    public final void APISendCredentials() {
        client.setUser("username");
        client.setPassword("password");


    }

    @Override
    public void onTestFailure(ITestResult result) {
        APISendCredentials();
        data.put("status_id", new Integer(5));
        data.put("comment", "This test failed");
        try {
            JSONObject r = (JSONObject) client.sendPost("add_result_for_case/" + "Unique ID", data);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (APIException e) {

            e.printStackTrace();
        }
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        APISendCredentials();
        data.put("status_id", new Integer(1));
        data.put("comment", "This test passed");
        try {
            JSONObject r = (JSONObject) client.sendPost("add_result_for_case/" + TestClass.AutomationID, data);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (APIException e) {

            e.printStackTrace();
        }

    }

    @Override
    public void onTestSkipped(ITestResult result) {
        APISendCredentials();
        data.put("status_id", new Integer(2));
        data.put("comment", "This test was blocked or skipped");
        try {
            JSONObject r = (JSONObject) client.sendPost("add_result_for_case/" + TestClass.AutomationID, data);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (APIException e) {
            e.printStackTrace();
        }

    }
}

      

Test class

package testPackage;

import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com.gurock.testrail.APIException;

@Listeners(MyTestResultsListener.class)

public class TestClass {

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestData {
    String testId() default "0";

}

public static String AutomationID = "236/50819";

@Test
@TestData(testId = "236/50819")
public void test1() {
    //sometest 

}

@AfterTest
public void aftertest() {
    //This was my initial blind attempt at reflection. I am getting 
    //an error below stating "test1" cannot be resolved to a variable 
    //also unclear on how to feed this to the APIclass 
    Method testMethod = getClass().getMethod(test1);
    if (testMethod.isAnnotationPresent(TestData.class)) {
        TestData testData = testMethod.getAnnotation(TestData.class);
        //stuck at this point 
    }
}

      

}

As you can see on the lines starting with JSONobject r

, I have a place for the uniqueID variable. What would be the best way / most efficient way to create a variable that could accept these Unqiue IDs from different test classes?

thanks, let me know if there is any additional information that would be helpful!

+3


source to share


1 answer


If the ID for each test does not change, you can create an annotation for example. @TestId

with the verification ID. Each test class (or method, depending on the desired level of detail) can be annotated with its own test ID. Then in the API code, you can just get it through reflection.



+2


source







All Articles