liveBook · Manning

Privileged access

As explained in Test Driven: Practical TDD and Acceptance TDD for Java Developers

A test pattern that accesses or modifies private state to make legacy code testable.

Privileged access is a test pattern for working around legacy code that cannot be changed. It uses Java’s Reflection API to read or modify private fields, making otherwise difficult tests possible. section 4.5.3

Inject.staticField("foo").of(LegacyCode.class).with(ourTestDouble);

BeanInject’s Inject utility substitutes a test double into a static field of a legacy class. This lets a test replace an internal dependency without changing the class. section 4.5.3

This workaround is especially useful when legacy code was not designed for testability. It is less useful when a project has used test-driven development from the start. section 4.5.3

How Test Driven: Practical TDD and Acceptance TDD for Java Developers covers it

How deep it goes
Explains the workaround and names utilities, with a one-line substitution example.
Examples
Shows BeanInject substituting a test double into a legacy class’s static field.
What you can do after
Apply it via BeanInject to substitute a test double into a private field.
How it connects
Connects reflection, test doubles, legacy code, and test-driven development.

This explainer was generated by Manning's knowledge graph from the text of Test Driven: Practical TDD and Acceptance TDD for Java Developers. The section links point to the parts of the book it draws on.