Course contentsShow
← Software Engineering
Lesson 484 of 1,93314. Test DoublesFree lesson

When to Use State vs Behavior Verification

Learn guidelines for choosing between state and behavior verification based on testing goals.

When to Use State vs Behavior Verification

What you'll learn: How to choose the right verification strategy to make your tests both meaningful and maintainable.

The Decision Point

You've seen state verification (checking outcomes) and behavior verification (checking interactions). But when should you use each? The choice depends on what matters most: what your code produces or how it collaborates.

Guidelines for Choosing

Use State Verification when:

  • The outcome is observable and meaningful (e.g., a calculation result, data transformation, or state change)
  • You're testing business logic that produces tangible results
  • You want tests that survive refactoring (state tests don't care how you got there)
  • The result is easy to assert (comparing values, checking object properties)

Think of it like judging a chef: you taste the dish, not watch every knife stroke.

Use Behavior Verification when:

  • The important thing IS the interaction itself (e.g., sending an email, logging an event)
  • Side effects are the primary concern with no easily observable state
  • You're testing at architectural boundaries (verifying communication between layers)
  • The real implementation is too costly or slow to use

This is like a driver's test: sometimes the instructor cares that you checked your mirrors, not just that you arrived safely.

The Pragmatic Balance

Prefer state verification as your default—it's less brittle and focuses on outcomes users care about. Reserve behavior verification for testing critical interactions that have no meaningful state to verify, or when you're testing how components work together across boundaries.

Key Takeaway: Use state verification to test outcomes and results; use behavior verification only when the interaction itself is what matters or when no meaningful state exists to verify.