Testing

Testing is crucial to our applications. When we use Spring MVC, we can count on the spring-test module to add support for unit and integration tests that are context-aware, which means that we can rely on annotations to wire dependencies. We can also use the @Autowired annotation to test a specific component.

The following is an example of how simple it is to write a test that is context-aware:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ContextAwareTest {

@Autowired
ClassUnderTest classUnderTest;


@Test
public void validateAutowireWorks() throws Exception {
Assert.assertNotNull(classUnderTest);
}
}

Let's review the code in bold, in order to understand how it works:

  • The first two annotations do all of the work for us; they will allow running our tests inside of a servlet container, and the Spring Boot annotations used for testing will wire all of the classes in the same way as the code running in production.
  • Since we added the previously mentioned annotations, we can now wire the components that we want to test using the @Autowired annotation.
  • The code validates that the class being tested has been successfully instantiated, and is ready to be used. This also means that all of the dependencies in the class have been successfully wired.

This is an easy way to test code that has to interact with databases, message broker servers, or any other middleware. The approach used to validate interactions with a database server uses an in-memory database, such as H2, for traditional SQL databases such as PostgreSQL or MySQL; there are also options for NoSQL databases, such as an embedded Cassandra or Mongo.

On the other hand, when you need to test integrations with other third-party software, an excellent approach to keep in mind is the use of sandboxes. A sandbox is an environment that is similar to the production environment, provided to software vendors for testing purposes. These sandboxes are often deployed in production, but they also have some restrictions. For example, operations related to payments are not processed in the last stage. 

This testing approach is useful when you don't have any way to deploy applications in your own environments, but of course, you will need to test whether the integrations are working with your applications.

Let's suppose that you are building an application that has integration with Facebook. In this case, it's evident that no change is required in order to deploy a Facebook instance in your own staging area for testing purposes. This is a perfect example of when a sandbox environment is appropriate.

Bear in mind that sandboxes test integrations using third-party software. If you're a software vendor, you need to consider providing sandboxes that allow your customers to try your products in a testing mode.

Spring MVC Test also has a fluent API that makes it possible to write highly expressive tests. This framework provides a MockMvc object that can be used to simulate end-user requests and then validate the provided responses. Common use cases include the following:

  • Validating HTTP code statuses
  • Verifying expected content in the responses
  • URL redirection

The following code snippet uses the MockMvc object to test the previously described examples:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class RedirectionTest
{
@Autowired
private MockMvc mockMvc;
@Test
public void contentAndRedirectionTest() throws Exception
{
this.mockMvc.perform(get("/urlPage"))
.andExpect(redirectedUrl("/expectedUrlPage")
.andDo(print()).andExpect(status().isOk())
.andExpect(
content().string(containsString("SomeText")))
);
}
}

Let's quickly review the code in bold, in order to understand how it works:

  • The AutoConfigureMockMvc annotation generates all of the required plumbing code to use the MockMvc object in the tests.
  •  The MockMvc object is autowired and ready to use.
  • The fluent API provided by MockMvc is used to validate the expected status code from the response. We are also testing a simple redirection, as well as the content expected on the page once the redirection is done.
..................Content has been hidden....................

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