From: Alban P. <alb...@fr...> - 2004-02-15 15:24:51
Attachments:
mockobjects-verifiables.patch
|
Hi, I used some private Mock classes in several projects. I'm refactoring my code to use less private classes. One of these seems useful outside my projects. So this is an helper class Verifiables (which could be part of com.mockobjects.util). The methods verify allow to verify an array or a Collection of Verifiables (like MockObjects for example). I use mostly these methods to verify several MockObjects at the end of a test case : Mock... mock1 = Mock... mock2 = Mock... mock3 = ... Verifiables.verify(new Verifiable[] { mock1, mock2, mock3 }); -- Alban Peignier - alb...@fr... http://www.tryphon.org/~alban |
From: Steve F. <st...@m3...> - 2004-02-15 15:58:28
|
Are you aware of the Verifier class which will verify all the Verifiable instance variables of a class? How many mocks do you have in your tests? Is it worth collecting them in an array, rather than just calling verify on them? It strikes me that a better alternative for your case might be to have a VerifiableCollection that accepts Verifiables and then implements verify() across them. S. Alban Peignier wrote: > Hi, > > I used some private Mock classes in several projects. I'm refactoring my > code to use less private classes. One of these seems useful outside my > projects. So this is an helper class Verifiables (which could be part of > com.mockobjects.util). The methods verify allow to verify an array or a > Collection of Verifiables (like MockObjects for example). > > I use mostly these methods to verify several MockObjects at the end of a > test case : > > Mock... mock1 = > Mock... mock2 = > Mock... mock3 = > > ... > > Verifiables.verify(new Verifiable[] { mock1, mock2, mock3 }); > > > ------------------------------------------------------------------------ > > Index: src/core/com/mockobjects/util/Verifiables.java > =================================================================== > RCS file: src/core/com/mockobjects/util/Verifiables.java > diff -N src/core/com/mockobjects/util/Verifiables.java > *** /dev/null 1 Jan 1970 00:00:00 -0000 > --- src/core/com/mockobjects/util/Verifiables.java 15 Feb 2004 11:52:15 -0000 > *************** > *** 0 **** > --- 1,51 ---- > + package com.mockobjects.util; > + > + import java.util.Arrays; > + import java.util.Collection; > + import java.util.Iterator; > + > + import com.mockobjects.Verifiable; > + > + /** > + * Provides helper methods to use <code>Verifiable</code> objects. > + * > + * @author <a href="mailto:alb...@fr...">Alban Peignier</a> > + */ > + public class Verifiables { > + > + private Verifiables() { > + } > + > + /** > + * Verifies a array of <code>Verifiable</code> instances. > + * <p/> > + * Useful to test easily several <code>MockObjects</code> at the end > + * of a test case : > + * <pre><code> > + * Mock... mock1 = ... > + * Mock... mock2 = ... > + * Mock... mock3 = ... > + * > + * ... > + * > + * Verifiables.verify(new Verifiable[] { mock1, mock2, mock3 }); > + * </code></pre> > + * @see #verify(Collection) > + */ > + public static void verify(Verifiable verifiables[]) { > + verify(Arrays.asList(verifiables)); > + } > + > + /** > + * Verifies a <code>Collection</code> of <code>Verifiable</code> instances. > + * > + * @see #verify(Verifiable[]) > + */ > + public static void verify(Collection verifiables) { > + for (Iterator iter=verifiables.iterator(); iter.hasNext(); ) { > + Verifiable verifiable = (Verifiable) iter.next(); > + verifiable.verify(); > + } > + } > + > + } |
From: Alban P. <alb...@fr...> - 2004-02-15 17:33:01
|
Steve Freeman wrote: > How many mocks do you have in your tests? Is it worth collecting them > in an array, rather than just calling verify on them? In some complex test cases, I can use five or six mockobjects. I found usefull to replace the needed lines to verify each one by a single one. > It strikes me that a better alternative for your case might be to have > a VerifiableCollection that accepts Verifiables and then implements > verify() across them. Indeed, A VerifiableCollection can be a smarter solution. This class doesn't seem to exist ? -- Alban Peignier - alb...@fr... http://www.tryphon.org/~alban |
From: Steve F. <st...@m3...> - 2004-02-15 18:17:07
|
Do you have multiple tests that use these mocks? In which case, our practice is to make them instance variables of the test case, and use the Verifier. We haven't got a VerifiableCollection yet, because we tend to use this solution. S. Alban Peignier wrote: > Steve Freeman wrote: > >> How many mocks do you have in your tests? Is it worth collecting them >> in an array, rather than just calling verify on them? > > > In some complex test cases, I can use five or six mockobjects. I found > usefull to replace the needed lines to verify each one by a single one. > >> It strikes me that a better alternative for your case might be to have >> a VerifiableCollection that accepts Verifiables and then implements >> verify() across them. > > > Indeed, A VerifiableCollection can be a smarter solution. This class > doesn't seem to exist ? > |