4

I'd like to test a find rest service, if I find smth I want to delete from the database, otherwise do nothing

I use it like this (where rs is the Response from find)

 JsonPath jsonPath = rs.getBody().jsonPath();
 Object foundName= jsonPath.get("name");

  if (foundName!= null) {

   expect().statusCode(200).when().delete("..." + foundName);

 }

So when nothing is found how to check the foundName for it , because I tried foundName!=null or foundName != "", and still it's not working. So please explain what is the structure of an empty response body

April
  • 435
  • 1
  • 5
  • 9
  • In java you cannot compare strings with `!=`. Use the `String.equals("")` method – Tim Apr 02 '14 at 10:46
  • yes, I tried that as well and foundName.equals("") is false – April Apr 02 '14 at 11:00
  • Try using a debugger to find the value of `foundName` – Tim Apr 02 '14 at 11:16
  • I tried debugging and foundName has value ArrayList with subelements : elementData = Object[0] (displayed value is []) , modCount = 0 , size = 0 – April Apr 02 '14 at 11:34
  • So obviously it's not a string as you expected. Now you're a step closer to finding the error do you think you can work from here? – Tim Apr 02 '14 at 11:37
  • Well, I still don't know how to correctly check foundName, so I would appreciate any suggestions – April Apr 02 '14 at 11:39

3 Answers3

3

Based on the debug info foundName is of type List , so the solution was to cast foundName to List and check if it's empty.

 List foundName = (List)jsonPath.get("name");
 foundName.isEmpty()
April
  • 435
  • 1
  • 5
  • 9
3
rs.body(blankOrNullString());

worked for me to verify the response body is null or blank.

ChrisM
  • 167
  • 1
  • 5
  • 12
0

You could call jsonPath.getString("name") which casts your (empty) response body to String and you could check it with equals("") (see RESTassured JavaDoc). I assumed that "name" is of type String.

lightwalker
  • 3
  • 1
  • 3
  • I tried that it like that as well, but jsonPath.getString() basicaly uses the same jsonPath.get() internally, so it's the same thing I tried with get() – April Apr 02 '14 at 12:08
  • I see. So what does the returned string contain? – lightwalker Apr 03 '14 at 07:34