Java Reference
In-Depth Information
{BrowserVersion.FIREFOX_3},
{BrowserVersion.INTERNET_EXPLORER_6},
{BrowserVersion.INTERNET_EXPLORER_7}});
}
E
public
JavadocPageAllBrowserTest(BrowserVersion browserVersion) {
this
.browserVersion = browserVersion;
}
F
@Test
public void
testSearchPage()
throws
Exception {
WebClient webClient =
new
WebClient(
this
.browserVersion);
// same as before...
}
}
Based on our previous example, we made the following changes: We used the
Parameterized
JU
nit test runner
b
. We added a BrowserVersion instance variable
C
to track the browser context. We added the method
getBrowserVersions
D
to return a list of
BrowserVersion
objects corresponding to the browsers we want to
test. The signature of this method must be
@Parameters
public
static
java.util.
Collection
, without parameters. The
Collection
elements must be arrays of identi-
cal lengths. This array length must match the number of arguments of the only pub-
lic constructor. In our case, each array contains one element because the public
constructor has one argument.
Now let's step through the test.
JU
nit calls the static method
getBrowserVersions
C
.
JU
nit loops for each array in the
getBrowserVersions
collection
D
.
JU
nit calls the
only public constructor
E
. If there's more than one public constructor,
JU
nit will
throw an assertion error.
JU
nit calls the constructor with an argument list built from
the array elements. In our case,
JU
nit calls the one argument constructor with the only
element in the array.
JU
nit then calls each
@Test
method
F
as usual. We repeat the
process for the next array in the
getBrowserVersions
collection
D
.
When you compare the test results with the previous example, you'll see that
instead of running one test, the parameterized
JU
nit test runner ran the same method
four times, once for each value in our
@Parameters
collection.
12.3.4
Creating standalone tests
You may not always want to use actual
URL
addressed pages as test fixtures,
HTTP
,
files, or otherwise. Next, we show you how to embed and run
HTML
in the unit test
code itself.
The framework allows you to plug a mock
2
HTTP
connection into a web client. In
listing 12.3, we set up a mock connection with a default
HTML
response string. The
test can then get this default page by using any
URL
value.
2
See chapter 7, “Testing with mock objects.”