permaculture playing cards*
The moose likes Testing and the fly likes How to unit test a value object that depends on services? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to unit test a value object that depends on services?" Watch "How to unit test a value object that depends on services?" New topic
Author

How to unit test a value object that depends on services?

songo han
Greenhorn

Joined: Mar 07, 2014
Posts: 4
If I have the following value object class PhoneNumber:



If I wanted to test my PhoneNumber class correctly I would need to Stub both Pattern and Matcher classes so that the code is tested in isolation. However, since I'm not injecting them into my class I have no way to alter the implementation of the classes. I'm tied to these types.

Maybe I should have injected them through my constructor, but that way I would be injecting an injectable into a newable according to Misko Hevery To "new" or not to "new"....

How do you test such class in isolation?

note that this is only an overly simplified example. There could be many rules for validating my value object and some serious logic that need proper testing.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
Welcome to JavaRanch.
I would need to Stub both Pattern and Matcher classes so that the code is tested in isolation.

Why? Those classes are part of the JRE, you're not testing them (and I certainly wouldn't call them "services"). I see nothing wrong with instantiating them - you can't possibly avoid instantiating any classes that are part of the JRE.


Ping & DNS - my free Android networking tools app
songo han
Greenhorn

Joined: Mar 07, 2014
Posts: 4
Why? Those classes are part of the JRE, you're not testing them (and I certainly wouldn't call them "services"). I see nothing wrong with instantiating them - you can't possibly avoid instantiating any classes that are part of the JRE.


Well to tell you the truth I'm still learning my way into proper unit testing. In numerous books I read about how unit testing should be done in isolation. Meaning that every test should test only one unit in isolation from its collaborators.

That's why people use Stubs/Mocks to fake collaborators and test the class in isolation. How can I be sure that my test isn't failing because there is a bug in the Parser or Matcher classes?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
Those classes don't change. If there's is a bug in them, you'd find it during your normal development and testing activities (unless it's an obscure one, but then the unit test wouldn't find it either).
The OpenJDK project is sure to have plenty of unit tests for those classes that are way more elaborate than anything you would ever come up with.

That article talks about classes you write, or classes that might differ between development and production (maybe using a test service vs. a real service), or classes that might not be present in a development or test environment (like an HttpServletRequest if you're writing servlets). It does not apply to these objects that are the same everywhere.
songo han
Greenhorn

Joined: Mar 07, 2014
Posts: 4
I see your point. So if I use a JRE class then it's safe to just use it directly in my test cases.

But what if I used a custom validator class that I built like this:

Since PhoneValidator is a custom class that I built it may have some errors. What should I do then?

This is just a simplification as in real life I may need more complex objects than a validator.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
I still see nothing wrong with instantiating that class. You may want to use a factory instead, if there can be different PhoneValidator implementations (in which case PhoneValidator would likely be an interface, or a superclass), and you want to unit test all of them.

Note how that article talks specifically about a situation where the object that would be used in production (the MusicPlayer) can't be instantiated in the test environment, because the hardware is not available. So that would be similar to the HttpServletRequest situation I mentioned.
songo han
Greenhorn

Joined: Mar 07, 2014
Posts: 4
aah I see. Now it makes since. Thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to unit test a value object that depends on services?
 
Similar Threads
Please tell me whether this procedure is correct or not.
Regex for capital letters
Regex Question - Quantifiers' behavior.
Help in Regex
Please help me check this regex