7 Coarse-grained testing with stubs

 

This chapter covers

  • Testing with stubs
  • Using an embedded server in place of a real web server
  • Implementing unit tests of an HTTP connection with stubs

And yet it moves.

--Galileo Galilei

As you develop your applications, you will find that the code you want to test depends on other classes, which themselves depend on other classes, which then depend on the environment (figure 7.1). You might be developing an application that uses Hibernate to access a database, a Java EE application (one that relies on a Java EE container for security, persistence, and other services), an application that accesses a filesystem, or an application that connects to some resource by using HTTP or another protocol.

Figure 7.1 The application depends on other classes, which in turn depend on other classes, which then depend on the environment.

Starting in this chapter, we will look at using JUnit 5 to test an application that depends on external resources. We will include HTTP servers, database servers, and physical devices.

7.1 Introducing stubs

7.2 Stubbing an HTTP connection

7.2.1 Choosing a stubbing solution

7.2.2 Using Jetty as an embedded server

7.3 Stubbing the web server resources

7.3.1 Setting up the first stub test

7.3.2 Reviewing the first stub test

7.4 Stubbing the connection

7.4.1 Producing a custom URL protocol handler

7.4.2 Creating a JDK HttpURLConnection stub

7.4.3 Running the test

Summary