Search code examples
javafitnesse

How to pass Java null strings to Fitnesse


Following the Fitnesse TestBlankAndNullCells documentation, I am trying to pass null fields into the verify tables. My data types are Strings.

When I leave the field blank, there is a solid red square with no information. When I try adding "null," the actual is empty. The same problem occurred using "blank". Am I missing something?

Here is more documentation on it, which states the keywords "blank" or "null" are required. I was able to manually return the string "null" as the actual, which works when the expected is also the literal "null," but I am hoping to find a cleaner solution in which I can pass in null fields.

According to this question on the Fitnesse forums, it looks like Strings are treated differently with null values.


Solution

  • I found a solution after browsing the Fitnesse forums, straight from one of the developers for Fit, Rick Mugridge:

    public String findString(String s) { 
        if ("".equals(s)) 
          return null; 
        return s; 
      } 
    } 
    

    I included this in the ArrayFixture and it worked! Here are some interesting points from Rick's message:

    When the myList() method is called for the second table, FitLibrary automatically wraps it with a SetFixture (because the returned value is a Set). When it checks each of the name elements, which are of type String, it determines that there is a findString() method of the right types, so it calls it to parse the value from the cells in the name column. That method turns an empty string into a null so that it will match correctly with the actual value of null.

    • For any type T, FitLibrary checks for a method public Object findT(String s). It calls it to parse the String value from a table cell. This can be used in several ways.

    • SetFixture, ArrayFixture, etc will access private instance variables if necessary (eg, no getter)

    • Arrays, Lists, Maps, Sets and other Objects are all auto-wrapped with an appropriate fixture in flow

    • Flow occurs when the first fixture in a DoFixture (In the next release, a DoFixture doesn't need to be the first fixture for flow to occur; it just starts later.)