Review of test coverage and testability

When we introduced our sample service, we identified several issues related to testing. The first of these issues was the lack of isolation, where tests for one layer were also indirectly testing all the layers below it, as shown in the following code:

func TestGetHandler_ServeHTTP(t *testing.T) {   // ensure the test always fails by giving it a timeout   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)   defer cancel()   // Create and start a server   // With out current implementation, we cannot test this handler without    // a full server as we need the mux.   address, err := startServer(ctx)   require.NoError(t, err)   // build inputs   response, err := http.Get("http://" + address + "/person/1/" ...

Get Hands-On Dependency Injection in Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.