• 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

JUnit and multiple classes

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I was curious as to what your thoughts are on the following scenario with regards to unit testing. If an Article class calls upon a method in a customized Utility class, would it still be good practice to write a test
within ArticleTest to verify that the customized Utility method was properly executed? Or, would it be best to write separate test cases (e.g. ArticleTest and CustomUtilityTest for each class)? I am leaning towards the latter as it is conventional to have one TestCase per Java class. Thanks much for your help!

Tavia
 
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
I would say that you're leaning in the right direction. I'm always suspicious of "Utility"-type classes and since you're debating whether to test it together with the other class, there could be a hint of a code smell there. Could it be that the code in your utility class is not really a "utility" method. Does it suffer from feature envy?
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Junilu. I think it makes good sense to test the customized Utility class with a separate TestCase; however, could you elaborate a bit more on "feature envy"?

I'm also curious if there is a case where you would want to test two classes together? If so, how would one go about it?

Thanks again!
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tavia Young:
could you elaborate a bit more on "feature envy"?


Feature Envy means that a class A is using class B's features so much that it might make sense to move those features into class A.
 
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

Originally posted by Lasse Koskela:

Feature Envy means that a class A is using class B's features so much that it might make sense to move those features into class A.



Or move the "envious code" from class A to class B.

I'll try to post a good example later.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Junilu Lacar:
Or move the "envious code" from class A to class B.


Doh [slaps forehead]

Yes, obviously that is the other option.
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Lasse and Junilu. If you could post an example, that would be great. =)
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One example of Feature Envy coming up...

Here's a class named Square, which represents (drum roll, please) a square:

Now, here's a class that uses the Square class above in a manner that smells like Feature Envy:

The use of multiple getter methods on the object being passed to a method often indicates that it would make sense to use the Move Method refactoring. In our example, that would mean moving the calculateCenter(Square square) method into the Square class:
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much, Lasse. =)

On an related (or unrelated) note, how would one go about testing a class that is declared as an Interface? Thanks again!
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tavia,
An interface doesn't have any implementation code (as it is just a declaration of signatures) so there isn't anything to test. You would test the code that uses the interface the same way as "regular" code.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you *can* do for the interface is write tests for the contract all implementations should obey. See http://c2.com/cgi/wiki?AbstractTestCases
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
You would test the code that uses the interface the same way as "regular" code.



Thanks very much, Jeanne & Ilja.

I realize that I should really be testing the code which makes use of the Interface, as Jeanne mentioned above. For example, if I were to test the Struts FormFile, how would one declare this Interface (i.e. The code I would like to test has some operations performed on a FormFile object.)?

Thanks again!
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tavia Young:
For example, if I were to test the Struts FormFile, how would one declare this Interface (i.e. The code I would like to test has some operations performed on a FormFile object.)?


When the code under test uses an interface, you generally either
1) create a static mock implementation of that interface, geared for testing only, or
2) use a mock implementation of that interface from an existing library such as mockobjects.com (if the interface is a standard java.* or javax.* interface), or
3) use a dynamic mock object library such as EasyMock or JMock to create a mock implementation of the interface in your test code
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch, Lasse. JMock seems like an interesting tool to use. Will investigate some more...
 
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 Tavia Young:
Thanks a bunch, Lasse. JMock seems like an interesting tool to use. Will investigate some more...



Personally, I prefer EasyMock over JMock - it's a little bit less flexible (though still flexible enough for 80% of the time) and does use actual method calls on the mock to "program" expectations, so that it goes smoother with automated refactorings. That is, if you rename a method on the Interface you mock, the JMock test will likely fail, whereas the EasyMock test will simply get refactored along.

EasyMock also is a little bit less verbose in most cases, so it seems to me.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fully agree with Ilja regarding EasyMock vs JMock. In addition, EasyMock actually has documentation which cannot be said about JMock...
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guy are fabulous! I agree - there doesn't seem to be much documentation available for JMock.

What are your views on units tests for reading and writing files (i.e. the classes in java.io.*)? Thanks again! =)
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tavia Young:
What are your views on units tests for reading and writing files (i.e. the classes in java.io.*)?


Don't test them. You have to take for granted that the J2SE core classes have been implemented correctly...

Then again, if you meant testing your own class such as

which uses java.io.*, then I would probably write unit tests for that code.
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes good sense, Lasse.

In reference to the above messages re Interfaces and Easy Mock/JMock, does it make more sense to test the struts FormFile class or the implementation of the class, CommonsFormFile? I always get so confused when it comes to java IO and file uploads!
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tavia Young:
does it make more sense to test the struts FormFile class or the implementation of the class, CommonsFormFile?


Always use the level of abstraction your code under test uses. If you have a method like
public String doSomethingToFormFile(FormFile ff)
you use EasyMock/JMock to generate a mock object for FormFile, not for some specific implementation of FormFile.
 
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 Tavia Young:
What are your views on units tests for reading and writing files (i.e. the classes in java.io.*)? Thanks again! =)



Actual file access is rather slow and unreliable. I typically prefer to code against streams, readers or writers instead, and use the appropriate in-memory implementations (ByteArray* or String*) for testing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic