Skip to content

Commit

Permalink
Fix errors in Testing chapter
Browse files Browse the repository at this point in the history
- group code example callouts to ensure callouts are displayed for the
  correct examples

- add missing callouts

- fix syntax, annotation attribute names, etc.
  • Loading branch information
sbrannen committed Nov 28, 2022
1 parent 3afc6a5 commit 2847621
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Expand Up @@ -1429,6 +1429,7 @@ Now we can use WebDriver as we normally would but without the need to deploy our
application to a Servlet container. For example, we can request the view to create a
message with the following:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -1440,9 +1441,11 @@ message with the following:
----
val page = CreateMessagePage.to(driver)
----
--

We can then fill out the form and submit it to create a message, as follows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -1456,13 +1459,15 @@ We can then fill out the form and submit it to create a message, as follows:
val viewMessagePage =
page.createMessage(ViewMessagePage::class, expectedSummary, expectedText)
----
--

This improves on the design of our <<spring-mvc-test-server-htmlunit-mah-usage, HtmlUnit test>>
by leveraging the Page Object Pattern. As we mentioned in
<<spring-mvc-test-server-htmlunit-webdriver-why>>, we can use the Page Object Pattern
with HtmlUnit, but it is much easier with WebDriver. Consider the following
`CreateMessagePage` implementation:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -1551,11 +1556,12 @@ by the `id` or `name` of the element within the HTML page.
https://github.com/SeleniumHQ/selenium/wiki/PageFactory#making-the-example-work-using-annotations[`@FindBy` annotation]
to override the default lookup behavior. Our example shows how to use the `@FindBy`
annotation to look up our submit button with a `css` selector (*input[type=submit]*).

--

Finally, we can verify that a new message was created successfully. The following
assertions use the https://assertj.github.io/doc/[AssertJ] assertion library:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -1568,10 +1574,12 @@ assertions use the https://assertj.github.io/doc/[AssertJ] assertion library:
assertThat(viewMessagePage.message).isEqualTo(expectedMessage)
assertThat(viewMessagePage.success).isEqualTo("Successfully created a new message")
----
--

We can see that our `ViewMessagePage` lets us interact with our custom domain model. For
example, it exposes a method that returns a `Message` object:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -1589,12 +1597,14 @@ example, it exposes a method that returns a `Message` object:
----
fun getMessage() = Message(getId(), getCreated(), getSummary(), getText())
----
--

We can then use the rich domain objects in our assertions.

Lastly, we must not forget to close the `WebDriver` instance when the test is complete,
as follows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -1616,6 +1626,7 @@ as follows:
}
}
----
--

For additional information on using WebDriver, see the Selenium
https://github.com/SeleniumHQ/selenium/wiki/Getting-Started[WebDriver documentation].
Expand Down
Expand Up @@ -644,7 +644,7 @@ path that represents a resource URL (i.e., a path prefixed with `classpath:`, `f
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from "/app-config.xml" and
// "/test-config.xml" in the root of the classpath
@ContextConfiguration("/app-config.xml", "/test-config.xml") // <1>
@ContextConfiguration(locations = ["/app-config.xml", "/test-config.xml"]) // <1>
class MyTest {
// class body...
}
Expand All @@ -667,7 +667,7 @@ demonstrated in the following example:
// class body...
}
----
<1> Specifying XML files without using the `location` attribute.
<1> Specifying XML files without using the `locations` attribute.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
Expand All @@ -678,7 +678,7 @@ demonstrated in the following example:
// class body...
}
----
<1> Specifying XML files without using the `location` attribute.
<1> Specifying XML files without using the `locations` attribute.


If you omit both the `locations` and the `value` attributes from the
Expand Down Expand Up @@ -743,6 +743,7 @@ The following example shows how to specify Groovy configuration files:
// class body...
}
----
<1> Specifying the location of Groovy configuration files.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
Expand Down
Expand Up @@ -222,6 +222,7 @@ resource base path). The resource base path is used behind the scenes to create

The following example shows how to use the `@WebAppConfiguration` annotation:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -231,6 +232,7 @@ The following example shows how to use the `@WebAppConfiguration` annotation:
// class body...
}
----
<1> The `@WebAppConfiguration` annotation.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
Expand All @@ -242,13 +244,15 @@ The following example shows how to use the `@WebAppConfiguration` annotation:
}
----
<1> The `@WebAppConfiguration` annotation.
--


To override the default, you can specify a different base resource path by using the
implicit `value` attribute. Both `classpath:` and `file:` resource prefixes are
supported. If no resource prefix is supplied, the path is assumed to be a file system
resource. The following example shows how to specify a classpath resource:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -270,6 +274,7 @@ resource. The following example shows how to specify a classpath resource:
}
----
<1> Specifying a classpath resource.
--


Note that `@WebAppConfiguration` must be used in conjunction with
Expand Down Expand Up @@ -1104,7 +1109,7 @@ annotation. The following example shows how to declare an SQL group:
@SqlGroup({ // <1>
@Sql(scripts = "/test-schema.sql", config = @SqlConfig(commentPrefix = "`")),
@Sql("/test-user-data.sql")
)}
})
void userTest() {
// run code that uses the test schema and test data
}
Expand Down

0 comments on commit 2847621

Please sign in to comment.