Java Reference
In-Depth Information
Listing 12.3
Configuring a standalone test
[...]
public class InLineHtmlFixtureTest {
@Test
public void testInLineHtmlFixture() throws Exception {
final String expectedTitle = "Hello 1!";
String html = "<html><head><title>" + expectedTitle +
"</title></head></html>";
WebClient webClient = new WebClient();
MockWebConnection conn = new MockWebConnection();
conn.setDefaultResponse(html);
webClient.setWebConnection(conn);
HtmlPage page = webClient.getPage(" http://page");
Assert.assertEquals(expectedTitle, page.getTitleText());
webClient.closeAllWindows();
}
[...]
We start by defining our expected HTML page title and HTML test fixture. Then we
create the web client, a MockWebConnection b , and install the HTML fixture as the
default response for the mock connection C . We can then set the web client's con-
nection to our mock connection D . We're now ready to go, and we get the test page.
Any URL will do here because we set up our HTML fixture as the default response.
Finally we check that the page title matches our HTML fixture.
To configure a test with multiple pages, you call one of the MockWebConnection
setResponse methods for each page. The code in listing 12.4 sets up three web pages
in a mock connection.
B
C
D
Listing 12.4
Configuring a test with multiple page fixtures
@Test
public void testInLineHtmlFixtures() throws Exception {
WebClient webClient = new WebClient();
final URL page1Url = new URL(" http://Page1/");
final URL page2Url = new URL(" http://Page2/");
final URL page3Url = new URL(" http://Page3/");
MockWebConnection conn = new MockWebConnection();
conn.setResponse(page1Url,
"<html><head><title>Hello 1!</title></head></html>");
conn.setResponse(page2Url,
"<html><head><title>Hello 2!</title></head></html>");
conn.setResponse(page3Url,
"<html><head><title>Hello 3!</title></head></html>");
webClient.setWebConnection(conn);
B
C
HtmlPage page1 = webClient.getPage(page1Url);
Assert.assertEquals("Hello 1!", page1.getTitleText());
D
HtmlPage page2 = webClient.getPage(page2Url);
Assert.assertEquals("Hello 2!", page2.getTitleText());
 
 
 
Search WWH ::




Custom Search