views:

118

answers:

1

I have a simple hibernate mapping file that makes use of a custom value type for one of the fields. The custom value type contains some reasonably complex business logic. I would like to be able to unit test my object persistence without the custom value type's logic. Is there any way to mock out the custom value type?

I have seen a similar question to this where the accepted solution was to enhance the custom value type to accept an injected 'strategy'. I don't particularly like the idea of enhancing my class just to cater for unit testing, so I would prefer to mock if possible.

Thanks to anyone for any help!!

+1  A: 

I'm not quite clear on what is it you intend to test?

If you're actually going to persist your entity, you won't be able to mock Hibernate's custom type - at least not in the common sense of what "mocking" is; all UserType methods must return meaningful stuff. That's the main reason for "injected strategy" solution - doing it any other way would result in more work :-)

Now, you can most certainly write an extremely simplistic implementation of UserType and substitute it for your original type in test configuration, but I'm not sure how that's going to work for the actual persistence - presumably that "complex logic" is there for a reason and trying to work around it may result in DB-level errors. If that's not the case, than perhaps that logic has no place in custom type and instead belongs somewhere in the service (business) layer.

ChssPly76
Thanks for your answer. Just to clarify, I have custom value types that make service calls in order to map a java field to one/many datablase fields. An example that I am plucking from thin air would be if you had a zip code field in a java object that you wanted to map into street, town, state fields in the DB table. Could use a custom value type that makes a call to some zipcode look-up service. If I now want to unit test the persistence of my java object, I would like to avoid hitting the zipcode service, so would like to be able to 'mock' the custom value type. Hope this makes sense!!
fivemile
Thanks for the clarification. I think something like that would be best done above the persistence layer to be honest. That aside, however, in this scenario it seems like you'd be mocking the zipcode service rather than your custom type. I'm not sure whether you're injecting it or using some sort of service locator but either way it should be reasonably straightforward to replace the actual implementation with mock.
ChssPly76