|
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();
> + }
> + }
> +
> + }
|