CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Dave Laribee

"Whoso would be a man must be a nonconformist." - Ralph Waldo Emerson

Test Your NHibernate Mappings!

Disclaimer! Ayende rightly points out that if you are testing your queries you get this mapping test for free. If you aren't, well put this in place as a stop gap measure (to test mappings) and if you have queries, which you probably do, get to testin'!

A while back Bil Simser put up a post on the first test you should write when using Castle Windsor. In it he shows a technique for verifying his component registrations are correct.

Since Windsor and NHibernate are the peanut butter and jelly of the ALT.NET crowd, I think we can and should do the same for NHibernate:


[Test]
public void Verify_NHibernate_mappings()
{
   string myConfigPath = @"wherever\you\put\your\NHibernate\config.xml";
   Configuration config = new Configuration();
   _sessionFactory = config.Configure(nhibernateConfig).BuildSessionFactory();
}

Would that I did this before today. Yeah, I win the douchey team member award for making a bunch of refactorings (renames mostly) and not fixing the mappings. Remember: Resharper, as great as it is, doesn't do NHibernate mappings. Of course the unit tests passed so I think I'm fine and move on with my life. The next developer that updates with my commit can't run the app and gets to clean up my mess. Fun for them!

I've learned my lesson. This little screw up illustrates the need for at least some automated integration tests and the lynchpins of many of our architectures -- the ORM/IoC tools -- seem like easy and high value targets.



Comments

Bil Simser said:

Thanks Dave. You know, it never occurred to me to post this as well as I have this spec in our projects as well.

# June 17, 2008 10:40 PM

Steven Harman said:

>> Resharper, as great as it is, doesn't do NHibernate mappings.

I recently came across the ReSharper NHibernate Plugin, but haven't had a chance to dig into it yet. nhplugin.lieser-online.de

# June 17, 2008 11:20 PM

Dave Laribee said:

@Steve - Sweet! (Potentially.)

# June 17, 2008 11:25 PM

Ayende Rahien said:

Dave,

You have said that queries are business logic.

As such, they should be tested. And because DB logic and App logic are different, you need to test them against a DB.

They cannot be tested if the mapping is broken.

# June 17, 2008 11:49 PM

Reflective Perspective - Chris Alcock » The Morning Brew #117 said:

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #117

# June 18, 2008 2:07 AM

Patrick De Boeck said:

As already mentioned above the resharper nhibernate plugin.  

It is available at nhplugin.lieser-online.de

and gives you suppport for invalid mappings.  Of course you will need the unitttest, but it is nice to have a little bit of resharper support (Although it doesnt refactor the files for you)

# June 18, 2008 2:30 AM

Jason Imison said:

If you were using NHibernate.Mapping.Attributes, you wouldn't need to worry about your mapping files being out of sync.

# June 18, 2008 3:45 AM

Tobin Harris said:

Nice tip!

I usually keep a separate test fixture for *just* testing that NHIbernate mappings are working as expected (testing use of entities + associations). That tests my mapping file by default, but I'd not thought of testing it explicitly.

# June 18, 2008 5:14 AM

What integration tests should you write first « Valeriu Caraulean - Building Software said:

Pingback from  What integration tests should you write first « Valeriu Caraulean - Building Software

# June 18, 2008 6:22 AM

Trey Thornton said:

See Ben Sheirman's screencasts for a complete TDD with NH presentation.

# June 18, 2008 8:20 AM

Dave Laribee said:

@Ayende

Sure. Are the concepts of a) testing your mappings and b) testing your queries mutually exclusive?

Granted testing your queries automatically tests your mappings but we can get some advantage -- if we're being shameful and not testing queries -- by doing the mapping test first.

Oh, ok, I'll add a dislaimer at the top. I talked myself into it...

# June 18, 2008 9:15 AM

Will Shaver said:

The resharper nhibernate plugin is now on google code here:

code.google.com/.../nhplugin

I've been using it for about two weeks and find it handy. Not perfect yet, but does save time avoiding obvious spelling errors.

RelatedCustomer_DataList

v

RelatedCustomers_DataList

etc

# June 18, 2008 11:56 AM

Stefan Lieser said:

Hi Dave,

as was suggested already you could use the R# NHibernate plugin from code.google.com/.../nhplugin. It has support for renaming of mapped properties and fields. Yesterday I added support for mapped classes, the other type names will follow.

If you are missing a feature please feel free to drop me a note.

Cheers,

Stefan Lieser

# June 18, 2008 12:30 PM

Colin Jack said:

We have the test you describe and it could be useful, no point running the (relatively slow) persistence tests if the mappings are bad.

# June 18, 2008 1:46 PM

Jeremy D. Miller -- The Shade Tree Developer said:

Yesterday, David Laribee related some problems he experienced with refactorings in his domain model leading

# June 18, 2008 10:54 PM

Community Blogs said:

Yesterday, David Laribee related some problems he experienced with refactorings in his domain model leading

# June 18, 2008 11:01 PM

Test Your NHibernate Mappings! said:

Pingback from  Test Your NHibernate Mappings!

# June 19, 2008 11:21 AM

Thomas Mayfield said:

Some of the useful data out of NHibernate when you set up your session factory comes out as log4net warnings.  One in particular is when you use private setters on your data objects, but there are others.

To catch these, I have a custom log4net appender derived from their AppenderSkeleton that just adds all logging messages it receives to a list. I set the threshold to Warn, and run through my NH initialization.  If any warnings/errors are output, I say the unit test fails.

Important note if you go this route: I can't guarantee that log4net will stop writing to a closed/disposed appender. You're likely to (only sometimes) get an exception thrown for writing to a closed appender if you Dispose() yours.  Any hints would be great, but for now I just don't close my custom appender.  It doesn't hurt anything but memory usage to have it continue to collect logging data while the rest of the test suite runs, and that could easily be fixed by telling it to no longer collect events.

# July 11, 2008 9:01 AM

Dave Laribee said:

@Thomas - Sweet tip!

# July 11, 2008 11:48 AM

Directed Graph » Failing unit test for NHibernate configuration warnings/errors said:

Pingback from  Directed Graph » Failing unit test for NHibernate configuration warnings/errors

# July 11, 2008 1:35 PM

NHibernate « Beautiful code said:

Pingback from  NHibernate « Beautiful code

# August 6, 2008 2:51 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications