img
.
AbstractTransactionalJUnit4SpringContextTests class to clean up the table. Note that after calling the
save operation, we need to explicitly call the EntityManager.flush() method to force Hibernate to flush
the persistence context to the database so that the findAll() method can retrieve the information from
the database correctly.
In the second test method, the testAddContactWithJSR303Error() method, we test the save
operation of a contact with a validation error. Note that in the @Test annotation, an expected attribute is
passed, which specifies that this test case is expected to throw an exception with the specified type,
which in this case is the ConstraintViolationException class.
Run the test class again and verify the result in the JUnit view.
Note that we covered only the most commonly used classes within Spring's testing framework.
Spring's testing framework provides a lot of support classes and annotations that allow us to apply fine
control during the execution of the test case life cycle. For example, the @BeforeTransaction and
@AfterTransaction annotations allow certain logic to be executed before Spring initiates a transaction or
after a transaction is completed for the test case. For a more detailed description of the various aspects
of Spring's testing framework, kindly refer to Spring's reference documentation.
Implementing a Frontend Unit Test
Another testing area of particular interest is testing the frontend behavior as a whole, upon the
deployment of the web application to a web container like Apache Tomcat.
The main reason is that even though we test every layer within the application, we still need to make
sure that the views behave correctly with different actions from users. Automating frontend testing is
very important in saving time for developers and users when repeating the actions on the frontend for a
test case.
However, developing a test case for a frontend is a challenging task, especially for those web
applications with a lot of interactive, rich, and Ajax-based components.
In the following sections, we will discuss implementing frontend unit testing with Selenium
(http://seleniumhq.org), a popular open source framework for automated frontend testing.
Adding Required Dependencies
To use Selenium, we need to add the dependency into our project, as shown in Table 19-4.
Table 19-4. Maven Dependencies for Selenium
Group ID
Artifact ID
Version
Description
2.15.0
Selenium library for Java
org.seleniumhq.selenium
selenium-java
Introducing Selenium
Selenium is a powerful and comprehensive tool and framework target for automating web-based
frontend testing. The main feature is that by using Selenium, we can "drive" the browsers, simulating
user interactions with the application, and perform verification of the view status.
Selenium supports common browsers including Firefox, IE, and Chrome. In terms of languages,
Java, C#, PHP, Perl, Ruby, and Python are supported. Selenium is also designed with Ajax and rich
Internet applications (RIAs) in mind, making automated testing of modern web applications possible.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home