aspose file tools*
The moose likes Testing and the fly likes How best to represent tabular data in a Unit Test method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How best to represent tabular data in a Unit Test method" Watch "How best to represent tabular data in a Unit Test method" New topic
Author

How best to represent tabular data in a Unit Test method

Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1173
    
  65

Hi folks,

I have just written a handful of Unit Tests that test the processing of an external data feed coming from some remote system over Http. In order to isolate myself from the remote system I have injected a Test Double that looks up some tabular data from a local CSV file instead so that I have control over the input data and am not reliant on the availability of this external system. This works fine.

However, upon review I have realised that my tests are exhibiting a readability smell that Lasse Koskela refers to as "Split Logic" (in his most excellent book "Effective Unit Testing"). The problem is that the information relating to the test is split between the test case itself and the CSV file which makes it difficult to determine what the test is actually doing. As the data sets are expected to be very small, just a handful of rows, I would like to inline the data.

I am currently on the road to addressing this issue and have written a new Test Fake class that I can load up with some pre-defined data from my test case. I am now defining my dataset in the test case, which is what I wanted, but the format of loading up Lists with the data is really ugly and I have been unable to find a way to represent the data in a way that makes the test methods naturally readable.

Has anybody run into this sort of problem before who may be able to give me some suggestions on how to represent my data in Java while retaining the visually tabular format to keep my tests readable?

Thanks
Tim


Tim Driven Development
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

You kind of lost me in the vagueness of the second and third paragraph. Can you give an example of the ugly format that you have? Obfuscate the test values if you must but I think a small example will help clarify the problem.


Junilu - [How to Ask Questions] [How to Answer Questions]
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1173
    
  65

Yea OK, fair enough.

Having started with loading the dataset from a csv file:

default,itemCode,itemName
false,001,WidgetOne

I have inlined it into the test case. Currently my test case looks much like this:

  • importDataService is the class under test.
  • createFakeDataDAOWithStaticData is a private method that creates a Test Fake version of a DataDAO pre-loaded with the given data.

  • Functionally this works quite nicely but my problem is that it is not very readable. It's not immediately obvious what data we are creating, and that's with only one item in the dataset. What I would like to do is to be able to refactor the method so that the dataset is presented visually in some tabular form. For example:

    While this looks a lot better, the first downside that I can see is that an "auto format" from an IDE will get rid of all the extra whitespace I've added to get the columns to line up nicely.
    Having written this as an example I realise that I'm a good way to solving my own problem but it would be very interesting to hear if anyone else has written similar tests and how you did it?
    Junilu Lacar
    Bartender

    Joined: Feb 26, 2001
    Posts: 5018
        
        8

    You're going to think this is really stupid, but my first thought is to just disable autoformat. If you use Eclipse 3.6 and greater:

    You can Google for it for more details.

    Barring that, I would try method chaining and trimming of values:

    The builder methods take variable length argument lists and trim each value. You'd still want to disable or at least customize the formatting with this.
    Yes, a little goofy maybe but that's the price you pay for being anal about formatting Hey, you and me both, buddy.
    Tim Cooke
    Bartender

    Joined: Mar 28, 2008
    Posts: 1173
        
      65

    Oh nice. The method chaining idea looks interesting. I'll definitely give that a go. Thanks very much for the reply.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: How best to represent tabular data in a Unit Test method