I am exploring Java and RestAssured, where I was trying to attach a document to my bug in JIRA.
While I was able to attach a text file successfully(jira.txt), I re-executed the code to attach the excel and I get FilenotFoundException error while trying to insert an excel file format.
But, both the document path is under the same root folder of project-JIRA-API. Please advise.
Here is a snippet: (the code runs fine when it is "jira.txt" )
//Add an attachment
System.out.println("\n\n...............ADD AN ATTACHMENT TO BUG..........\n\n");
given().log().all().header("X-Atlassian-Token","no-check").filter(session)
.header("Content-Type","multipart/form-data")
.multiPart("file",new File("API.xlsx"))
.when().post("rest/api/2/issue/" id "/attachments")
.then().log().all().assertThat().statusCode(200);
( link below for code screenshot)code snippet
CodePudding user response:
There are 2 ways to deal with file path:
Solution 1:
- Put the file in
src/test/resources
- Code will be
.multiPart("file",new File("src/test/resources/API.xlsx"))
Solution 2:
- Put the file in working directory
- Code will be
.multiPart("file",new File(System.getProperty("user.dir") "/API.xlsx"))
CodePudding user response:
Thank you. I still faced FileNotfound exception for both of above. However, when I gave the link from my system directory now, it surprisingly worked.
But, I am still trying to figure out on the exception/issue while trying to attach from eclipse directory. Because it works for txt file. Not sure what am I missing out for the excel format.
Code:
//Add an attachment
System.out.println("\n\n...............ADD AN ATTACHMENT TO BUG..........\n\n");
File testUploadFile= new File ("E:\\API learning\\API.xlsx");
given().log().all().header("X-Atlassian-Token","no-check").filter(session)
.header("Content-Type","multipart/form-data")
.pathParam("id", "10101")
// .multiPart("file",new File(System.getProperty("user.dir") "/API.xlsx"))
.multiPart(testUploadFile)
.when().post("rest/api/2/issue/{id}/attachments")
.then().log().all().assertThat().statusCode(200);