# PHPUnit\Framework\MockObject\RuntimeException: Trying to configure a method "getUser" that does not exist on mock class "App\Service\UserService"

- **ID:** `php/phpunit-mock-builder-method-exists`
- **Domain:** php
- **Category:** test_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

PHPUnit's mock builder is trying to configure a method that does not exist on the mocked class, usually due to a typo in the method name or the method being private/final/static.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| PHPUnit 10.5 | active | — | — |
| PHPUnit 11.0 | active | — | — |

## Workarounds

1. **Verify the method name spelling and visibility. Run `php -r "echo (new ReflectionMethod('App\\Service\\UserService', 'getUser'))->isPublic() ? 'public' : 'not public';"` to check if the method exists and is public.** (95% success)
   ```
   Verify the method name spelling and visibility. Run `php -r "echo (new ReflectionMethod('App\\Service\\UserService', 'getUser'))->isPublic() ? 'public' : 'not public';"` to check if the method exists and is public.
   ```
2. **If the method is static or final, use `onlyMethods()` with the method name, or refactor the class to make the method non-final. Example: `$mock = $this->getMockBuilder(UserService::class)->onlyMethods(['getUser'])->getMock();`** (80% success)
   ```
   If the method is static or final, use `onlyMethods()` with the method name, or refactor the class to make the method non-final. Example: `$mock = $this->getMockBuilder(UserService::class)->onlyMethods(['getUser'])->getMock();`
   ```

## Dead Ends

- **Adding a @runInSeparateProcess annotation to the test method to isolate state** — This does not fix the method name mismatch; it just runs the test in a separate process, which may mask the error but the underlying issue persists. (90% fail)
- **Using createMock() instead of getMockBuilder() assuming it's a builder issue** — createMock() also uses the same method existence check internally; the error will still occur if the method does not exist. (85% fail)
