简体   繁体   中英

How to submit test result from Java to a RESTful web service?

I have a testing framework where we use batch scripts. It runs XML files that are parsed by Java to run batch, PowerShell, and TestComplete scripts.

We have a RESTful web service. I would like to send the test results (how many test case passed, test case failed, and name) to this RESTful service automatically every time we run the tests.

In my Java code I already have the variables that hold the pass and fail results. The RESTful web service also uses XML or JSON to parse the data being posted. I just need some code that sends the needed data, like:

<?xml version="1.0" encoding="UTF-8"?>
<automation-report-request xmlns="urn:/lalal/lalalal/">
  <products>
     <product>...</product>
     <product>...</product>
    <!--...more "product" elements...-->
  </products>
  <scripts>
    <script-name>...</script-name>
    <script-name>...</script-name>
    <!--...more "script-name" elements...-->
  </scripts>
  <versions>
    <version>...</version>
    <version>...</version>
    <!--...more "version" elements...-->
  </versions>
  <branches>
    <branch>...</branch>
    <branch>...</branch>
    <!--...more "branch" elements...-->
  </branches>
  <languages>
    <language>...</language>
    <language>...</language>
    <!--...more "language" elements...-->
  </languages>

In my main BAT.java file, I have the variables that hold the information each time a batch script is ran.

output = new BufferedWriter(new FileWriter(file, true));
output.write(Time_Stamp.Time() +":::"+Global_Data_Store.Priority+":::"+Global_Data_Store.Test_Spec_Name+":::"+Global_Data_Store.NewBuildName+":::"+Global_Data_Store.HostIP+":::"+Global_Data_Store.Test_Plan_Name+":::"+Global_Data_Store.Test_Plan_Owner+":::"+Global_Data_Store.Total_Number_Fail_Test_Steps+":::"+Global_Data_Store.Total_Number_Pass_Test_Steps+":::"+Global_Data_Store.Total_Number_Test_Steps+":::"+Global_Data_Store.TestPlanStatus+":::"+Global_Data_Store.Total_Number_Fail_Test_Cases+":::"+Global_Data_Store.Total_Number_Pass_Test_Cases+":::"+Global_Data_Store.Total_Number_Test_Cases+"\n");

output.close();
Global_Data_Store.Total_Number_Fail_Test_Steps  = "" + 0;
Global_Data_Store.Total_Number_Pass_Test_Steps  = "" + 0;
Global_Data_Store.Total_Number_Test_Steps       = "" + 0;

Global_Data_Store.Total_Number_Fail_Test_Cases  = "" + 0;
Global_Data_Store.Total_Number_Pass_Test_Cases  = "" + 0;
Global_Data_Store.Total_Number_Test_Cases       = "" + 0;

How can I send the data to the RESTful service?

You can use Spring Framework's RestTemplate for interacting with Restful service from Java. Please check out the example, it has both Rest Server side and Rest Client side code:

http://johnathanmarksmith.com/spring/java/javaconfig/programming/spring%20java%20configuration/spring%20mvc/web/rest/resttemplate/2013/06/18/how-to-use-spring-resttemplate-to-post-data-to-a-web-service/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM