{"record":{"id":"2c9a6a89b00112db","repo":"sebastianbergmann/phpunit","slug":"no-return-value-is-configured-for-s-s-and-ret","errorCode":null,"errorMessage":"No return value is configured for %s::%s() and return value generation is disabled","messagePattern":"No return value is configured for (.+?)::(.+?)\\(\\) and return value generation is disabled","errorType":"exception","errorClass":"ReturnValueNotConfiguredException","httpStatus":null,"severity":"error","filePath":"src/Framework/MockObject/Runtime/InvocationHandler.php","lineNumber":184,"sourceCode":"                    $this->assertionFailure = $e;\n                }\n            }\n        }\n\n        if ($exception !== null) {\n            throw $exception;\n        }\n\n        if ($hasReturnValue) {\n            return $returnValue;\n        }\n\n        if (!$this->returnValueGeneration) {\n            if (strtolower($invocation->methodName()) === '__tostring') {\n                return '';\n            }\n\n            throw new ReturnValueNotConfiguredException($invocation);\n        }\n\n        return $invocation->generateReturnValue();\n    }\n\n    /**\n     * @throws Throwable\n     */\n    public function verify(): void\n    {\n        foreach ($this->matchers as $matcher) {\n            $matcher->verify();\n        }\n\n        if ($this->assertionFailure !== null) {\n            throw $this->assertionFailure;\n        }\n    }","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Framework/MockObject/Runtime/InvocationHandler.php#L166-L202","documentation":"Thrown by InvocationHandler::invoke() when a method call on the double matched no expectation and return value generation is disabled, so PHPUnit has nothing to return. Mock objects (createMock / expects()) run with returnValueGeneration=false: every method that production code actually calls must be covered by a matching expectation with a will(...) action, or the test fails here instead of returning a silent default. __toString is the single special case and returns '' instead of throwing.","triggerScenarios":"Production code under test calls $mock->helper() while the test only configured ->method('other'); an expectation exists for helper() but its with()/parameter rule does not match the actual arguments, so no matcher applies; calling any method on a mock created with createMock() without configuring a return for it.","commonSituations":"Exercising more of the subject than the test author realized (helper/format/log methods called in passing); using a mock where a stub was intended — createStub() enables generation and returns defaults; refactoring production code so previously untested methods are now called on the mock.","solutions":["Add a matching expectation with a return value: $mock->method('helper')->willReturn(...).","If the arguments of the real call differ from your with() constraints, correct the constraints or use ->withAnyParameters().","If you only need indifferent default returns, use $this->createStub(C::class) instead of a mock with expects().","For mocks created via getMockBuilder(), avoid disabling return value generation, or use autoConfigure... i.e. configure returns for every method the subject calls."],"exampleFix":"// before\n$repo = $this->createMock(UserRepo::class);\n$service->run($repo); // run() internally calls findAll(), unconfigured\n\n// after\n$repo = $this->createMock(UserRepo::class);\n$repo->method('findAll')->willReturn([]);\n$service->run($repo);","handlingStrategy":"fallback","validationCode":"// before exercising the subject, enumerate every collaborator method it calls and stub it\nforeach (['findAll', 'find', 'save'] as $method) {\n    $repo->method($method)->willReturn($defaults[$method] ?? null);\n}","typeGuard":"// no language-level guard; instead choose the right factory:\n// createStub() enables return value generation, createMock() disables it\nfunction doubleWithDefaults(string $class): Stub\n{\n    return PHPUnit\\Framework\\TestCase::createStub($class); // safe default returns\n}","tryCatchPattern":"try {\n    $subject->run($mock);\n} catch (PHPUnit\\Framework\\MockObject\\ReturnValueNotConfiguredException $e) {\n    // read which method was called, add ->method(...)->willReturn(...), rerun\n}","preventionTips":["Use createStub() when you only need indifferent return values; use createMock() only when asserting calls.","When an expectation exists but the call still throws, suspect with() constraints that do not match the real arguments.","Read the exception — it names the exact class::method that lacked a return value."],"tags":["phpunit","mock-object","unconfigured-return","mock-vs-stub"],"backgroundTag":"missing-stub-return-value","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}