liveBook · Manning

Testability

As explained in Grokking Software Architecture

Testability is the degree to which software can be easily tested to verify its behavior and correctness.

If testing a report means waking up a live database, a small check can become slow and brittle. Testability is how easily you can write a test to verify that software behaves correctly. Design choices that let tests control dependencies make that job easier. section 2.4 section 2.4.3

A tightly coupled component creates or relies directly on a specific dependency. Dependency injection offers a way out: pass the dependency in instead. A test can then supply a fake with predictable behavior, while production supplies the real implementation. section 2.4.3

A report without a live database

A report generator that creates its own database connection cannot easily control the data returned in a unit test. Accepting the connection through its constructor lets a test inject a fake that returns three rows and check the resulting report. section 2.4.3

Listing 2.5 "After": Loosely Coupled (reportGenerator.js)
class ReportGeneratorAfter {  #A
  constructor(dbConnection) {  #A
    // The dependency is "injected" into the constructor.  #A
    this.dbConnection = dbConnection;  #A
  }   #A
 
  generate(reportName) {
    const data = this.dbConnection.getData(reportName);
    return `Report '${reportName}' generated with ${data.length} rows.`;
  }
}

See the report generator accept its database connection instead of creating one. section 2.4.3

Listing 2.6 “After”: Passing Tests! (reportGenerator.test.js)
// Import class 
const { ReportGeneratorAfter } = require('./reportGenerator'); 
 
test('Generate_Report_With_Fake_Data', () => {  #A
  // Create a simple "mock" database connection for our test  #A
  const fakeDb = {   #A
    getData: (query) => ["row1", "row2", "row3"]  #A
  };   #A
  
  // Inject the fake dependency
  const reportGenerator = new ReportGeneratorAfter(fakeDb);
 
  // Act
  const result = reportGenerator.generate("TestReport");
 
  // Assert
  expect(result).toBe("Report 'TestReport' generated with 3 rows.");
});

See a test provide three fake rows and check the report output. section 2.4.3

The same idea applies beyond databases. An order service can receive a fake logger to check that it attempts to log without writing a file. Separating order validation from payments and notifications also lets each piece be tested in isolation. section 4.5.3 section 3.5.4

At a larger scale, hexagonal architecture keeps business logic inside a core and uses ports and adapters to connect it to the outside world. Fake adapters can exercise that logic without a database or user interface. The tradeoff is deliberate: extra interfaces, constructors, or files add setup and organization work, but make dependencies easier to replace and pieces easier to test. section 5.11 section 4.5.3 section 3.5.4

A useful design check: can you test the behavior you care about without reaching a live network or database? If not, look for a dependency that could be supplied from outside or a responsibility that could be separated. section 10.4.1 section 2.4.3 section 3.5.4

How Grokking Software Architecture covers it

How deep it goes
Works through dependency injection and fake-based unit tests, then connects isolated testing to service boundaries and hexagonal architecture.
Examples
Uses a report generator with a fake database, an order service with a fake logger, and a server monitor with a fake alert port.
What you can do after
Apply dependency injection and fake dependencies to unit-test behavior without live infrastructure.
How it connects
Connects testability to dependency injection, loose coupling, separation of concerns, and hexagonal architecture.

Also explained in

Compare how 7 books explain testability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.