Introduction to Spring Test

Spring a module called spring-test, which supports unit and integration testing of Spring components. Among other features, this module provides the ability to create Spring application context for testing purposes or create mock objects that to test our code in isolation. There are different annotations supporting this testing capabilities. A list of the most significant one is the following:

  • @ContextConfiguration: This annotation is used to determine how to load and configure an ApplicationContext for integration tests. For example, it allows to load the application context from annotated classes (using the element classes) or bean definitions declared in XML files (using the element locations).
  • @ActiveProfiles: This annotation is used to instruct the container about which definition profiles should be active during the application context loading (for example, development and test profiles).
  • @TestPropertySource: This annotation is used to configure the locations of the properties files and the inline properties to be added.
  • @WebAppConfiguration: This annotation is used to instruct the Spring context that ApplicationContext loaded is WebApplicationContext.

In addition, the spring-test module offers several capabilities to carry out different actions typically required in tests, namely:

  • The org.springframework.mock.web package contains a set of Servlet API mock objects, useful for testing web contexts. For instance, the object MockMvc allows to perform HTTP requests (POST, GET, PUT, DELETE, and so on) and verify the response (status code, content type, or response body).
  • The org.springframework.mock.jndi package contains an implementation of the Java Naming and Directory Interface (JNDI) SPI, which can be used to set up a simple JNDI environment for tests. For instance, using the class SimpleNamingContextBuilder we can make a JNDI data source available in our tests.
  • The org.springframework.test.jdbc package contains the class JdbcTestUtils, which is a collection of JDBC utility functions aimed to simplify standard database access.
  • The org.springframework.test.util package contains the class ReflectionTestUtils, which is a collection of utility methods to set a non-public field or invoke a private/protected setter method when testing the application code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.37.136