Java Reference
In-Depth Information
}
}
We have marked this method with the @Test annotation. We assume that an array of objects
that support the Batter interface has been initialized elsewhere, and go through that array and
check that the slugging percentage is consistent with the at-bats and the total number of bases.
We use a particular version of the assertEquals() method that allows us to supply a range
of accuracy that we consider acceptable. In this case, we consider a variation of .02 between
the reported averages to be acceptable.
Setting up the array of objects used in this test can be done in a method marked with the an-
notation @Before , which will be run by the JUnit framework before the other tests. This meth-
od can call out to other methods if needed, and is often the most difficult part of writing the
test, since this is where you will create the data on which the tests will depend. For our case,
we might start our test file with something like:
package org.oreilly.javaGoodParts.examples.impl;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter.AtBatResult;
/**
*
*/
public class BatterImplTest {
private BatterImpl[] testBatters;
private Random dataGen = new Random(1);
/**
* Set up the test array testBatters. We will test on 100 {@link BatterImpl}
* objects. These will each be initialized in a separate routine.
*/
@Before
public void setUp() {
Search WWH ::




Custom Search