• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How best to represent tabular data in a Unit Test method

 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 5555
    326
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh nice. The method chaining idea looks interesting. I'll definitely give that a go. Thanks very much for the reply.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic