The Artima Developer Community
Sponsored Link

Oh Boy! Another Testing Tool!
Building Conformance and Unit Tests with Artima Testkit
by Bill Venners

<<  Page 4 of 8  >>

Advertisement

Creating a Fixture

A fixture consists of objects and anything else needed to perform a test. In general, all test methods in a Suite will share the same fixture. Fixtures will be composed of objects, stored in private instance variables of the Suite. You can create fixtures in either of two ways, either via the constructor of your Suite subclass or via Suite's setupFixture method.

Suite.executeTestMethods calls setupFixture before invoking each test method, and calls cleanupFixture after each test method completes. Similar to JUnit's setup and teardown, these methods can be used to create a fresh fixture before each test method, and destroy it afterwards. The setupFixture and cleanupFixture methods are useful when your test methods destroy or change the fixture. Unlike JUnit, Testkit's Suite.executeTestMethods invokes all test methods on the same Suite instance. (JUnit generally creates a different TestCase instance for each test method.) If any of your test methods destroy its fixture such that the fixture can't be used by sibling test methods invoked later on the same Suite object, you should use setupFixture to create the fixture. In setupFixture, you create the necessary objects and perform any other tasks to ready the fixture, such as opening a file or socket. In cleanupFixture, you can release the objects to the whims of the garbage collector and perform any other necessary cleanup, such as closing the file or socket.

If you are certain your test methods won't destroy the fixture, and you don't need to perform any fixture cleanup, you can simply initialize the private variables that represent your fixture in a constructor of your Suite subclass. This is the approach I took in class LocalesSuite. I used the constructor to create the fixture objects, which I stored in private instance variables. I then made sure my test methods used, but did not destroy or change, the fixture objects. For the LocalesSuite fixture, I created five Locales objects and three HashSets, as shown in this code snippet from LocalesSuite:

package com.artima.serviceuitest;

import com.artima.testkit.Suite;
import net.jini.lookup.ui.attribute.Locales;
import java.util.HashSet;

public class LocalesSuite extends Suite {

    private Locales one;
    private Locales two;
    private Locales three;
    private Locales four;
    private Locales five;

    private HashSet setOne;
    private HashSet setTwo;
    private HashSet setThree;

    public LocalesSuite() {

        // one and two are equal and empty
        one = new Locales(new HashSet());
        two = new Locales(new HashSet());

        setOne = new HashSet();
        setOne.add(Locale.ENGLISH);

        // three has one locale in it
        three = new Locales(setOne);
        setTwo = new HashSet();
        setTwo.add(Locale.ENGLISH);
        setTwo.add(Locale.UK);

        // four has two locales in it
        four = new Locales(setTwo);

        setThree = (HashSet) setOne.clone();
        setThree.add(Locale.UK);

        // five has same two locales in it as four
        five = new Locales(setThree);
    }

<<  Page 4 of 8  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use