Tuesday, June 06, 2006

To mock or not to mock

this is posted in http://forums.lhotka.net/forums/2/1384/ShowThread.aspx#1384


To mock or not to mock: We really need to be careful here. We need to put things in perspective. TDD is THE agile and THE lightweight methodology – that is, before .net getting into the picture, i.e., when we are talking only in C++/Java context. Now, .net brings in VB tradition. VB has been in a RAD context, which is much lighter.

Before you dismiss my point lightly, note that I used Java and I love Java. Also, I used TDD and I love TDD. Of course, I also used classic VB, VB.net and C#.

My point: if you really want the lightweight end of TDD, then, do not mock. Mock is for developing frameworks, not for developing everyday applications.

A healthy way to resist the temptation of using mocks in developing everyday applications, your can participate some open source framework developments. In that way, you learn how to mock, the right place, and not mess up your everyday application developments.

Note that once you mock, your unit testing code is totally different from your real code; as a result, your unit testing code loses a key functionality: documentation.

9 Comments:

Blogger Vikas said...

Here are my deliberations on mock objects

Vikas

http://vikasnetdev.blogspot.com/2006/05/when-to-use-mock-objects.html

6/10/2006 06:21:00 AM  
Blogger survic said...

I double posted this on both here and Vikas's blog.

http://vikasnetdev.blogspot.com/2006/05/when-to-use-mock-objects.html
---------------

Hi Vikas:

We come to the same conclusion, and you beat me again -- you posted it on May 11!

It is not easy to “be different” from both sides: classical VB (it tends to dismiss unit testing all together) and the heavy-duty TDD.

I noticed your earlier posts about mock. You tried and used mock objects. It is excellent. It means your conclusion is tested and reliable.

The way I see it, because developing a framework (or a big utility) is not the norm, to spread the good news of TDD, we need to take the mock out of it -- no pun intended :-)
Put it another way, I am pretty much convinced that introducing TDD with mock is not a good idea.

Just to balance my tone, I feel I must say this also: mock is really an intriguing technique. I am especially interested in it because of “dynamic proxy” and AOP. I feel I should work on the castle framework more.

It is really nice hearing similar voices on such an important yet kind of ambiguous issue.

6/10/2006 11:10:00 AM  
Blogger Vikas said...

Hi Survic,

I agree with you viewpoint that mock is not something that you could use in .net in every day’s life
I remember that last time I worked with J2EE, you could not test JDBC calls using JUnit. You have to use container based testing framework like Cactus which was very 2 slow. IMO this is where this concept came that database calls are part of integration test not unit test because it was not possible in JUnit. Correct me if I am wrong.
In Nunit , one can write tests and test database functionality and it is pretty fast. Not everything that worked for J2EE will work for .Net. One has to perform Cost Benefit Analysis before jumping to conclusion.

One more example, every JAVA project was following J2EE specification and was taking performance hit because of heavy container. When J2EE community realized that not every project needs heavy-weight Container and cost associated with it., they came with lightweight container called Spring.

So blindly following every JAVA practice will be suicidal.

Having said that mock objects can be very useful in following situation

1. When you need some global objects which are hard to create. You don’t want to get into details of application framework object creation. You just want to set two or three properties of these Objects to test your business objects because your business objects need them.
2. Second situation would be if you are dependent on external factors. If you need to call a third party api or webservice that returns you some values. You can use Mock these calls so that you are not dependent on third party to test your component.. I think that is IOC situation.
3. to mock objects like UI ( I don’t see myself using it in near future)

6/15/2006 08:31:00 PM  
Blogger survic said...

I agree that if we say “copy everything from Java”, then, it must be the “lightweight” java, i.e., no EJB, especially no entity bean (if you do not need 2pc, no session bean either). However, to be fair to entity bean, it certainly has contributed to the death of stored procedure approach in java world -- the final strike was, of course, the real OR mapping, those Hibernate-likes.

I understand that “copy everything from Java” will be less and less popular in .net world, as the .net world is getting closer to its teenage years, and it is rightly so – for example, I feel closer to Castle than to springframework.

However, .net is not really reaching its teenage yet. We still need “copy everything from lightweight Java”. A lot of issues still need some leap of faith of java experiences. For example, stored procedure vs. OR mapping, dataset etc. Another example is the strong typed collection. I am very happy that generics finally puts the end of such ugly stuff. Yet another example is using the “command pattern” as the foundation of distributed framework (yes, I am talking about CSLA). In java world, this was very popular in its early days, predated J2EE. The ambitious IBM’s San Francisco project was based on it. However, then, it was replaced by J2EE, and in lightweight Java, it was replaced by dynamic proxy approach. My prediction is that CSLA will be replaced by Castle-like based systems -- that is if CSLA does not evolve into that by itself. I hope CALS community can take or adapt to such a leap -- I have my doubts; but I really hope it can make it.

All in all, .net will need about 3 years to catch up lightweight Java in enterprise computing; in other words, we will need “copy everything from lightweight Java” for another three years.

I agree Cactus is pain – that is one of the problem in ejb: it is not easy to test. If you use lightweight java (i.e., hibernate-like OR mapping plus dynamic proxy for façades – note that those can be done easily in-house, spring framework is just "nice to have"), then, it is very much like Castle-dynamic proxy .net development. Testing database code is similar.

Note that technically speaking, castle-dynamic proxy corresponds to java’s cglib; however, for façade, in java, java’s build-in dynamic proxy is sufficient; but there is no equivalent thing in .net. As a result, in .net, to get to the level of façade proxy, we have to get the level equiv. to java’s cglib, which is castle’s dynamic proxy. So, from the point of view of the necessity of getting to facade proxy, .net’s Castle-like-dynamic proxy corresponds to Java’s build-in dynamic proxy.


As for why mock is so popular in Java. My guess is that it is simply because they have time to waste. Usually java projects are much larger than .net projects; they can accommodate something like that; however, I do not believe using mock is really justifiable even in java world, if the project is not a framework making project.

6/17/2006 12:14:00 AM  
Anonymous Anonymous said...

Devonshire!
[URL=http://xanax-info.mlvma.com/info-klikt0p-levitra.html-site-xanax---.html ] info klikt0p levitra.html site xanax [/URL]
[URL=http://lamisil-pill.xffkv.com/lamisil-pill-prices--.html ] lamisil pill prices [/URL]
[URL=http://alternative-medicine-for-asthma.mlvma.com/alternative-medicine-for-asthma--.html ] alternative medicine for asthma [/URL]
[URL=http://pharmacy-technician-continuing-education.xffkv.com/continuing-education-pharmacy-technician--.html ] continuing education pharmacy technician [/URL]
[URL=http://cialis-information.mlvma.com/cialis-drug-information.html ] cialis drug information [/URL]
[URL=http://order-carisoprodol-online.xffkv.com/order-cheap-carisoprodol-online---.html ] order cheap carisoprodol online [/URL]
[URL=http://drug-guide-interaction.mlvma.com/ ] drug guide interaction [/URL]
[URL=http://dental-office-marketing.mlvma.com/dental-marketing-office.html ] dental marketing office [/URL]

Know what I mean?
http://ultram-50mg.mlvma.com/50mg-tramadol-ultram--.html 50mg tramadol ultram
http://pharmacy-career-information.uefhv.com/pharmacy-career-information--.html pharmacy career information
http://cialis-com.uefhv.com/cialis-com-drug-generic-rxpricebusters--.html cialis com drug generic rxpricebusters
http://medicine-hat-ab.uefhv.com/ab-hat-medicine-news.html ab hat medicine news

[LINK=http://prescription-drug-discount-card.xffkv.com ] prescription drug discount card [/LINK]

12/02/2006 10:50:00 PM  
Anonymous Anonymous said...

By Gaia, but it's years stale in here!
[URL=http://hampton-university-school-of-pharmacy.uefhv.com/hampton-university-school-of-pharmacy--.html ] hampton university school of pharmacy [/URL]
[URL=http://walgreen-39-s-pharmacy.xffkv.com/walgreen-39-s-pharmacy--.html ] walgreen 39 s pharmacy [/URL]
[URL=http://order-tenuate.uefhv.com/link-online.html-order-tenuate-viagra.orxc--.html ] link online.html order tenuate viagra.orxc [/URL]
[URL=http://board-dental-exam-hygiene.mlvma.com/dental-hygiene-board-exam.html ] dental hygiene board exam [/URL]
[URL=http://adipex-non-prescription.mlvma.com/adipex-non-prescription--.html ] adipex non prescription [/URL]
[URL=http://alternative-medicine-holistic-medicine.mlvma.com/alternative-holistic-medicine-veterinary---.html ] alternative holistic medicine veterinary [/URL]
[URL=http://canada-drug-in-store.mlvma.com/drug-store-in-canada--.html ] drug store in canada [/URL]
[URL=http://ambien-free-trial.xffkv.com/ ] ambien free trial [/URL]

Had they not once made the long haul through the mountain chain maybe four hundred klicks south of there, and stood looking out over the seething Pacific Ocean, watching it roil and bubble and steam?
http://celebrex-heart.xffkv.com/celebrex-heart-problem---.html celebrex heart problem
http://england-journal-medicine-new-research.xffkv.com/ england journal medicine new research
http://celexa-effexor.mlvma.com/ celexa effexor
http://herbs-alternative-medicine-health.xffkv.com/alternative-health-herb-medicine--.html alternative health herb medicine

[LINK=http://living-with-a-drug-addict.mlvma.com ] living with a drug addict [/LINK]

12/04/2006 12:48:00 PM  
Anonymous Anonymous said...

Him of all men!
[URL=http://injection-testosterone.uefhv.com/ester-injection-testosterone---.html ] ester injection testosterone [/URL]
[URL=http://aflac-dental-insurance.xffkv.com/ ] aflac dental insurance [/URL]
[URL=http://canada-cialis.uefhv.com/cialis-in-canada.html ] cialis in canada [/URL]

doesn't have?
http://nurse-drug-guide.xffkv.com/nurse-drug-guide--.html nurse drug guide
http://buy-celexa.xffkv.com/buy-celexa-where.html buy celexa where

[LINK=http://family-dental-care.xffkv.com ] family dental care [/LINK]

12/09/2006 06:20:00 PM  
Anonymous Anonymous said...

Ryan, you old bastard!
[URL=http://college-dental-ontario-royal-surgeon.mlvma.com/royal-college-of-dental-surgeon-of-ontario.html ] royal college of dental surgeon of ontario [/URL]
[URL=http://effects-of-celebrex.mlvma.com/celebrex-effects-effects-from-side-vioxx.html ] celebrex effects effects from side vioxx [/URL]
[URL=http://100mg-tramadol.mlvma.com/tramadol-100mg.html ] tramadol 100mg [/URL]

What's your specialty?
http://lippincotts-nursing-drug-guide.uefhv.com/2006-drug-guide-lippincotts-nursing-pda.html 2006 drug guide lippincotts nursing pda
http://effects-of-celebrex.mlvma.com/celebrex-effects-effects-from-side-vioxx--.html celebrex effects effects from side vioxx

[LINK=http://natural-testosterone-supplement.uefhv.com ] natural testosterone supplement [/LINK]

12/11/2006 09:44:00 PM  
Anonymous Anonymous said...

Who'd they freeze, Doc?
[b][url="http://hydrocodone.dewall.info "]oxycodone vs hydrocodone[/url][/b]

12/25/2006 12:55:00 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home