How to add test results in Azure DevOps through REST API java?

Ram :

I've tried with program below but test results are not added in VSTS:

public static void addTestResults() throws ClientProtocolException, IOException {
     String pat = "[email protected]:xxxxxxxxxxxxxxxxxxxxxx";
     byte[] byteArray2 = Base64.encodeBase64(pat.getBytes());
     String encodedString2 = new String(byteArray2);

     HttpClient httpClient = HttpClientBuilder.create().build();
     HttpPost postRequest = new HttpPost("https://xxx.visualstudio.com/DefaultCollection/XXX/_apis/test/runs/1000012/results?api-version=5.0-preview.5" );
     List<NameValuePair> arguments1 = new ArrayList<NameValuePair>(4);
     arguments1.add(new BasicNameValuePair("state", "Completed"));
     arguments1.add(new BasicNameValuePair("testPoint", "{\"id\":40}"));
     arguments1.add(new BasicNameValuePair("outcome", "Passed"));
     arguments1.add(new BasicNameValuePair("testCase", "{\"id\":7340}"));

     try {
         postRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
         patchRequest.setHeader(HttpHeaders.ACCEPT, "application/json");
         postRequest.setHeader("Authorization", "Basic "+encodedString2);
         postRequest.setEntity(new UrlEncodedFormEntity(arguments1,"UTF-8"));

         HttpResponse response = httpClient.execute(postRequest);
         System.out.println("Out is"+EntityUtils.toString(response.getEntity()));

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

Following is the output:

Out is
{  
"$id":"1",
"innerException":null,
"message":"Value cannot be null.\r\nParameter name: results",
"typeName":"System.ArgumentNullException, mscorlib",
"typeKey":"ArgumentNullException",
"errorCode":0,
"eventId":0
}

Can anyone help me out where I am going wrong. I am trying to solve it for days but in vain.

Ram :

I got the solution and here it is:

public static void addTestResults() throws ClientProtocolException, IOException {
     String pat = "[email protected]:xxxxxxxxxxxxxxxxxxxxxx";
     byte[] byteArray2 = Base64.encodeBase64(pat.getBytes());
     String encodedString2 = new String(byteArray2);
    String reqbody = "{\n" + 
            "        \"state\": \"Completed\",\n" + 
            "        \"testPoint\": {\n" + 
            "          \"id\": 40\n" + 
            "        },\n" + 
            "        \"outcome\": \"Passed\",\n" + 
            "        \"testCase\": {\n" + 
            "          \"id\": 7390\n" + 
            "        }\n" + 
            "      }";
     HttpClient httpClient = HttpClientBuilder.create().build();
     HttpPost postRequest = new HttpPost("https://xxx.visualstudio.com/DefaultCollection/XXX/_apis/test/runs/1000012/results?api-version=5.0-preview.5" );


     try {
         postRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
         patchRequest.setHeader(HttpHeaders.ACCEPT, "application/json");
         postRequest.setHeader("Authorization", "Basic "+encodedString2);
         postRequest.setEntity(new StringEntity(reqbody, ContentType.APPLICATION_JSON));

         HttpResponse response = httpClient.execute(postRequest);
         System.out.println("Out is"+EntityUtils.toString(response.getEntity()));

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=83801&siteId=1