sebastianbergmann/phpunit · error · AssertionFailedError
Failed asserting that exception with code "%s" is thrown
Error message
Failed asserting that exception with code "%s" is thrown
What it means
Thrown by ExceptionExpectation::assertWasRaised() when expectExceptionCode() was configured but no exception was raised during the test. With nothing thrown there is no code to compare, so PHPUnit fails the test with the expected code in the message.
Source
Thrown at src/Framework/TestCase/ExceptionExpectation.php:172
sprintf(
'Failed asserting that exception with message %s "%s" is thrown',
$this->expectedMessageConstraint instanceof ExceptionMessageIs ? 'is' : 'containing',
$this->expectedMessage,
),
);
} elseif ($this->expectedMessageRegularExpression !== null) {
$test->addToAssertionCount(1);
throw new AssertionFailedError(
sprintf(
'Failed asserting that exception with message matching "%s" is thrown',
$this->expectedMessageRegularExpression,
),
);
} elseif ($this->expectedCode !== null) {
$test->addToAssertionCount(1);
throw new AssertionFailedError(
sprintf(
'Failed asserting that exception with code "%s" is thrown',
$this->expectedCode,
),
);
}
}
}
View on GitHub (pinned to f123cdb2a2)
Solutions
- Ensure the test input/arrangement actually causes the exception
- Remove expectExceptionCode() (and related expectations) if the non-throwing behavior is now correct, and assert the return value instead
Example fix
// before $this->expectExceptionCode(404); $repo->find($knownGoodId); // found, no exception // after $this->expectExceptionCode(404); $repo->find($missingId);
Defensive patterns
Strategy: validation
Prevention
- Arrange inputs that actually produce the exception whose code you expect
- Delete stale expectExceptionCode() calls when the corresponding error path is removed
When it happens
Trigger: Calling $this->expectExceptionCode(123) while the code under test runs to completion without throwing.
Common situations: Guards added to production code so the error no longer happens; tests for error codes where the error-triggering input was corrected; exception thrown in a dependent service that is now mocked to succeed.
Related errors
- Failed asserting that exception with message %s "%s" is thro
- Failed asserting that exception with message matching "%s" i
- Subscriber "%s" does not implement any known interface - did
- Unknown event type "%s"
- Subscriber "%s" does not implement a known interface
AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23).
Data as JSON: /api/errors/09dc85a8ce313dd2.
Report an issue: GitHub.