A stub is a test-friendly substitute for a dependency. It lets a scenario give the application a controlled response instead of relying on a real external service. This is an automation choice, not a requirement of BDD: the team can use real services or substitutes according to its strategy. section 12.5 section 15.7
A delivery that is late at 18:46
A customer expects a delivery at 18:45 and should receive a delay notification if it has not arrived by 18:46. Waiting for the clock to reach 18:46 would make the test slow. Instead, a stub time service lets the scenario set the time to 18:46 and trigger the time-change event. The application can then respond as though that time had arrived. section 15.7
The application and the stub use the same time-service interface. The stub returns its controlled current time and notifies subscribers when that time changes. A driver translates the scenario’s business-readable step into a call that sets the time; scenario setup configures the application to use the stub. Keeping those details behind the step preserves the scenario’s focus on the customer-facing behavior. section 15.7
Listing 15.23 Stub time service implementation
*** Support/StubTimeService.cs ***
public class StubTimeService : ITimeService
{
private DateTimeOffset now = DateTimeOffset.Now;
private readonly List<Func<DateTimeOffset, bool>> timeChangeSubscribers =
[];
public DateTimeOffset GetCurrentTime() => now;
public void SubscribeToTimeChange(
Func<DateTimeOffset, bool> onTimeChanged) =>
timeChangeSubscribers.Add(onTimeChanged);
public void SetCurrentTime(DateTimeOffset currentDateTime)
{
now = currentDateTime;
TriggerTimeChange();
}
public void TriggerTimeChange()
{
var currentDateTime = GetCurrentTime();
foreach (var subscriber in timeChangeSubscribers.ToArray())
{
if (subscriber(currentDateTime))
{
timeChangeSubscribers.Remove(subscriber);
}
}
}
}The stub time-service implementation shows both halves of the substitution: returning a settable time and triggering subscribed time-change callbacks. section 15.7
The same choice can apply to other external dependencies, including a database or payment provider. In a hexagonal architecture, an outbound port gives tests a place to connect either a real service adapter or a stub. Favor the arrangement that gives useful, fast, robust feedback for the scenario; the choice need not be identical for every dependency. section 15.7 section 15.8.1













