Join 34,000+ subscribers and receive articles from our
blog about software quality, testing, QA and security.

TestRail API add_run don't work in Java


#1

I try to add a new testrun via the TestRail API v2 method add_run in Java. It results in the following APIException:

Here is my Java code (I’m using Java :

public JSONObject AddTestRunTest(String sTRUrl, String sUserName, String sPassword ) { JSONObject jsonObject = null; APIClient client = new APIClient(sTRUrl); client.setUser(sUserName); client.setPassword(sPassword); String sjsonString = "{\"suite_id\": 166, " + " \"name\": \"New TestRun\", " + "\"assignedto_id\": 2 , " + "\"include_all\": false, " + "\"case_ids\": [18361, 18341, 18342, 18344, 18345, 18346, 18347, 18322, 18348, 18349, 18350, 18351, 18352] }"; try { String sAPIMethod = "add_run/18"; jsonObject = (JSONObject) client.sendPost(sAPIMethod, sjsonString ); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (APIException e) { e.printStackTrace(); } return jsonObject; }

Response-Code from method add_run 400 says: Invalid or unknown project.

BUT I’m sure that the project-, session- and even caseids are correct, existing and fit together.

I know this for sure, because if I do the same via curl it works.
Here my curl - command:

curl -D- -u Username:Password -X POST --data @create-testrun.json -H "Content-Type: application/json" TestRail-Host/testrail/index.php?/api/v2/add_run/18
Here the content of the file create-testrun.json (simply a copy of the content of my java-varaible - see above):

{"suite_id": 166, "name": "New TestRun", "assignedto_id": 2 , "include_all": false, "case_ids": [18361, 18341, 18342, 18344, 18345, 18346, 18347, 18322, 18348, 18349, 18350, 18351, 18352] }


#2

Hello Thomas,

Thanks for your posting. The Java API binding expects an object or array argument instead of a string. The binding would then automatically serialize this to a JSON string. An example for this can be found here:

http://docs.gurock.com/testrail-api2/bindings-java#examplepost_request

We recommend using Maps (for key/value lists) and Lists (for arrays) to format the arguments as demonstrated in the example.

I hope this helps and please let me know in case anything is unclear.

Regards,
Tobias


#3

Hello Tobias,

thanks a lot - it works now :smile:

Regard,
Thomas


#4

Hi Thomas,

Thanks for the update and great to hear that it works now! :smile:

Regards,
Tobias


#5

I kept using arrays until I came across this post where it suggested to use Lists instead. That solved my issue.
You should update your documentation on the Java examples.


#6

Hi Jesus,

Thanks for your feedback! Sure, we’re happy to review updating this and providing more examples in the future as well.

Regards,
Marco


#7

Actually, I found out it’s best to stay away from Lists and HashMaps. The provided JSON library suffices. Simply use JSONObject and JSONArray objects.