/** * Test sendResponse method with exception rendering. */ public function testSendResponseWithException() { /** Mock all required objects. */ $this->_rendererMock->expects($this->any())->method('getMimeType')->will($this->returnValue('application/json')); $this->_rendererMock->expects($this->any())->method('render')->will($this->returnCallback([$this, 'callbackForSendResponseTest'], $this->returnArgument(0))); $exceptionMessage = 'Message'; $exceptionHttpCode = \Magento\Webapi\Exception::HTTP_BAD_REQUEST; $exception = new \Magento\Webapi\Exception($exceptionMessage, 0, $exceptionHttpCode); $this->_errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception)); $this->_responseRest->setException($exception); /** Start output buffering. */ ob_start(); $this->_responseRest->sendResponse(); /** Clear output buffering. */ ob_end_clean(); $actualResponse = $this->_responseRest->getBody(); $expectedResult = '{"message":"' . $exceptionMessage . '"}'; $this->assertStringStartsWith($expectedResult, $actualResponse, 'Response body is invalid'); }