From: Nat P. <nat...@b1...> - 2003-12-03 17:49:06
|
On Wed, 2003-12-03 at 16:55, Mickael Vera wrote: > Hi everybody, > > I'm new to mockobjects and I try to understand to right way > to use them. > > What I understound of mockobjects is that the good way to > use them is to know which interactions it will have with > your tested object , then make your mockobject respond to the > tested object requests and record them, at the end of the test > check that what has been recorded is correct. Is it true ? Not exactly. Put assertions into your mocks, so that your mocks test that they are being called correctly at the point that the call occurs. This will give you much more useful information when a failure occurs. The Mock Objects jargon for an assertion in a mock object is an "expectation": it defines what you expects to happen to the mock during the test. > Is it so important to test interactions with mockobjects if your > tested method returns the values you expected ? Yes, because the calls that an object makes to other objects around it (its side effects) are part of its behaviour, and therefore something that you should test. > Is it correct sometimes to put code in your mockobject > that does the same as the object it replaces instead of returning > values. Sometimes it may be easier to code the functionnality than > to emulate responses. No. If it is that hard to mock a response you must be doing something wrong. Why is it so hard to mock responses? Let the "testability" of your code guide its design and the code will become easier to maintain as a result. > In my case I test services called from a servlet, so I manipulate > HttpRequest, HttpSession, ServletContext ... which I replaced with > mockobjects. > In this case I find it easier to code in the MockHttpSession the > setAttribute > and getAttribute methods than to guess which values will be asked if > the service puts a lot of objects in the session and remove them. > One service called by the servlet may have a lot of interactions > with mockobjects and I can't use real objects as I don't have > implementations. It sounds like you are doing too much in one test. In particular, it sounds like you are trying to do integration testing rather than unit testing. Try testing each of those objects individually, and isolate them using mock objects. Cheers, Nat. |