• 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

s'd i have the test as a static class inside the source file

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remeber having gone through a java tip that recommends writing test class as a static inner class inside the source file that we are testing.
The biggest advantage is that we have access to the outer class's private variables inside the test methods. That makes it easier to assert.
Had the test been outside the class , in extreme cases, i might have to change the access modifier of certain variables just to make sure that the test goes through fine. I guess that is not advisable.
What do u guys recommend?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main problem with with writing your tests as inner classes is that your test code is then tightly coupled with your production code.
  • You have to ship your test code along with your production code.
  • An inner class can bypass public accessors and possibly compromise the integrity of your test


  • I would generally recommend storing your tests in a parallel folder structure. e.g.:

    [ March 25, 2003: Message edited by: Rupert Thomas ]
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by karthik Guru:
    I remeber having gone through a java tip that recommends writing test class as a static inner class inside the source file that we are testing.
    The biggest advantage is that we have access to the outer class's private variables inside the test methods. That makes it easier to assert.


    I typically don't feel the need to access private variables (or methods) from my tests - and if I do, I take it as a hint that my design needs improvement. Most often, the class simply tries to do too much - there is one (or more) classes waiting to be extracted.
     
    Ilja Preuss
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Rupert Thomas:
    The main problem with with writing your tests as inner classes is that your test code is then tightly coupled with your production code.


    I wholeheartedly agree! The need for this is also a strong indicator that the production code itself is too tightly coupled, in my experience.


  • You have to ship your test code along with your production code.


  • As long as we are speaking about source code, I would advice to do this, anyway.
    If we are talking about class files, inner classes are compiled into there own ones and don't need to be distributed with the system. (In fact, the JVM doesn't know about inner classes at all.)
     
    Rupert Thomas
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    True enough...
    though I would still argue that, whether shipping or not, using distinct folder structures will make life a lot simpler and cleaner. You don't need to "pick apart" shipping and non-shipping classes (or sources).
    reply
      Bookmark Topic Watch Topic
    • New Topic