diff --git a/core/core.services.yml b/core/core.services.yml
index 737009bac5..4ce28dee43 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -986,7 +986,7 @@ services:
       - { name: event_subscriber }
   form_ajax_subscriber:
     class: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber
-    arguments: ['@form_ajax_response_builder', '@string_translation']
+    arguments: ['@form_ajax_response_builder', '@string_translation', '@messenger']
     tags:
       - { name: event_subscriber }
   route_enhancer.param_conversion:
diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php
index ee9078335b..411c3ef448 100644
--- a/core/lib/Drupal/Core/Block/BlockBase.php
+++ b/core/lib/Drupal/Core/Block/BlockBase.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\Access\AccessResult;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
 use Drupal\Core\Plugin\ContextAwarePluginBase;
 use Drupal\Component\Utility\Unicode;
@@ -26,6 +27,7 @@
 abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginInterface, PluginWithFormsInterface {
 
   use ContextAwarePluginAssignmentTrait;
+  use MessengerTrait;
   use PluginWithFormsTrait;
 
   /**
diff --git a/core/lib/Drupal/Core/Block/MessagesBlockPluginInterface.php b/core/lib/Drupal/Core/Block/MessagesBlockPluginInterface.php
index 995f4b72e0..9248ac1080 100644
--- a/core/lib/Drupal/Core/Block/MessagesBlockPluginInterface.php
+++ b/core/lib/Drupal/Core/Block/MessagesBlockPluginInterface.php
@@ -5,8 +5,7 @@
 /**
  * The interface for "messages" (#type => status_messages) blocks.
  *
- * @see drupal_set_message()
- * @see drupal_get_message()
+ * @see \Drupal\Core\Messenger\MessengerInterface
  * @see \Drupal\Core\Render\Element\StatusMessages
  * @see \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
  *
diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php
index c0d0cdd964..e114916c2c 100644
--- a/core/lib/Drupal/Core/Controller/ControllerBase.php
+++ b/core/lib/Drupal/Core/Controller/ControllerBase.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
 use Drupal\Core\Logger\LoggerChannelTrait;
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\Routing\LinkGeneratorTrait;
 use Drupal\Core\Routing\RedirectDestinationTrait;
 use Drupal\Core\Routing\UrlGeneratorTrait;
@@ -35,6 +36,7 @@
 
   use LinkGeneratorTrait;
   use LoggerChannelTrait;
+  use MessengerTrait;
   use RedirectDestinationTrait;
   use StringTranslationTrait;
   use UrlGeneratorTrait;
diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php b/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php
index 32c48d8a03..c87c08dc0a 100644
--- a/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php
+++ b/core/lib/Drupal/Core/Entity/ContentEntityDeleteForm.php
@@ -73,7 +73,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
       $form_state->setRedirectUrl($this->getRedirectUrl());
     }
 
-    drupal_set_message($this->getDeletionMessage());
+    $this->messenger()->addStatus($this->getDeletionMessage());
     $this->logDeletionMessage();
   }
 
diff --git a/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php b/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php
index 2faf45c44a..9761190572 100644
--- a/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php
+++ b/core/lib/Drupal/Core/Entity/EntityDeleteFormTrait.php
@@ -120,7 +120,7 @@ protected function logDeletionMessage() {
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
     $this->getEntity()->delete();
-    drupal_set_message($this->getDeletionMessage());
+    $this->messenger()->addStatus($this->getDeletionMessage());
     $form_state->setRedirectUrl($this->getCancelUrl());
     $this->logDeletionMessage();
   }
diff --git a/core/lib/Drupal/Core/Entity/EntityListBuilder.php b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
index 7191fae8d4..90b5dfce0d 100644
--- a/core/lib/Drupal/Core/Entity/EntityListBuilder.php
+++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\Core\Entity;
 
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\Routing\RedirectDestinationTrait;
 use Drupal\Core\Url;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -13,6 +14,7 @@
  */
 class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
 
+  use MessengerTrait;
   use RedirectDestinationTrait;
 
   /**
diff --git a/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php b/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php
index 3f8fd6d364..07b99b7d38 100644
--- a/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php
+++ b/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php
@@ -52,7 +52,7 @@ public function getFormId() {
   public function buildForm(array $form, FormStateInterface $form_state) {
     // Get all the available ways to transfer files.
     if (empty($_SESSION['authorize_filetransfer_info'])) {
-      drupal_set_message($this->t('Unable to continue, no available methods of file transfer'), 'error');
+      $this->messenger()->addError($this->t('Unable to continue, no available methods of file transfer'));
       return [];
     }
     $available_backends = $_SESSION['authorize_filetransfer_info'];
diff --git a/core/lib/Drupal/Core/Form/ConfigFormBase.php b/core/lib/Drupal/Core/Form/ConfigFormBase.php
index e7878a7db0..707e580d5a 100644
--- a/core/lib/Drupal/Core/Form/ConfigFormBase.php
+++ b/core/lib/Drupal/Core/Form/ConfigFormBase.php
@@ -51,7 +51,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    * {@inheritdoc}
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
-    drupal_set_message($this->t('The configuration options have been saved.'));
+    $this->messenger()->addStatus($this->t('The configuration options have been saved.'));
   }
 
 }
diff --git a/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php b/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php
index 6c9c1788f2..c8f2b6009e 100644
--- a/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php
+++ b/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php
@@ -9,6 +9,7 @@
 use Drupal\Core\Form\FormAjaxException;
 use Drupal\Core\Form\FormAjaxResponseBuilderInterface;
 use Drupal\Core\Form\FormBuilderInterface;
+use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\Core\StringTranslation\TranslationInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -30,6 +31,13 @@ class FormAjaxSubscriber implements EventSubscriberInterface {
    */
   protected $formAjaxResponseBuilder;
 
+  /**
+   * The messenger.
+   *
+   * @var \Drupal\Core\Messenger\MessengerInterface
+   */
+  protected $messenger;
+
   /**
    * Constructs a new FormAjaxSubscriber.
    *
@@ -37,10 +45,13 @@ class FormAjaxSubscriber implements EventSubscriberInterface {
    *   The form AJAX response builder.
    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
    *   The string translation.
+   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
+   *   The messenger.
    */
-  public function __construct(FormAjaxResponseBuilderInterface $form_ajax_response_builder, TranslationInterface $string_translation) {
+  public function __construct(FormAjaxResponseBuilderInterface $form_ajax_response_builder, TranslationInterface $string_translation, MessengerInterface $messenger) {
     $this->formAjaxResponseBuilder = $form_ajax_response_builder;
     $this->stringTranslation = $string_translation;
+    $this->messenger = $messenger;
   }
 
   /**
@@ -75,7 +86,7 @@ public function onException(GetResponseForExceptionEvent $event) {
     // Render a nice error message in case we have a file upload which exceeds
     // the configured upload limit.
     if ($exception instanceof BrokenPostRequestException && $request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) {
-      $this->drupalSetMessage($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]), 'error');
+      $this->messenger->addError($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]));
       $response = new AjaxResponse();
       $status_messages = ['#type' => 'status_messages'];
       $response->addCommand(new PrependCommand(NULL, $status_messages));
@@ -153,13 +164,4 @@ public static function getSubscribedEvents() {
     return $events;
   }
 
-  /**
-   * Wraps drupal_set_message().
-   *
-   * @codeCoverageIgnore
-   */
-  protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
-    drupal_set_message($message, $type, $repeat);
-  }
-
 }
diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php
index 02a1c3940c..591c82894e 100644
--- a/core/lib/Drupal/Core/Form/FormBase.php
+++ b/core/lib/Drupal/Core/Form/FormBase.php
@@ -6,6 +6,7 @@
 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
 use Drupal\Core\Logger\LoggerChannelTrait;
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\Routing\LinkGeneratorTrait;
 use Drupal\Core\Routing\RedirectDestinationTrait;
 use Drupal\Core\Routing\UrlGeneratorTrait;
@@ -45,6 +46,7 @@
   use DependencySerializationTrait;
   use LinkGeneratorTrait;
   use LoggerChannelTrait;
+  use MessengerTrait;
   use RedirectDestinationTrait;
   use StringTranslationTrait;
   use UrlGeneratorTrait;
diff --git a/core/lib/Drupal/Core/Form/FormErrorHandler.php b/core/lib/Drupal/Core/Form/FormErrorHandler.php
index 9783cb5c4a..0d7ff58d6a 100644
--- a/core/lib/Drupal/Core/Form/FormErrorHandler.php
+++ b/core/lib/Drupal/Core/Form/FormErrorHandler.php
@@ -3,6 +3,7 @@
 namespace Drupal\Core\Form;
 
 use Drupal\Component\Utility\NestedArray;
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\Render\Element;
 
 /**
@@ -10,6 +11,8 @@
  */
 class FormErrorHandler implements FormErrorHandlerInterface {
 
+  use MessengerTrait;
+
   /**
    * {@inheritdoc}
    */
@@ -39,7 +42,7 @@ protected function displayErrorMessages(array $form, FormStateInterface $form_st
 
     // Loop through all form errors and set an error message.
     foreach ($errors as $error) {
-      $this->drupalSetMessage($error, 'error');
+      $this->messenger()->addError($error);
     }
   }
 
@@ -160,13 +163,4 @@ protected function setElementErrorsFromFormState(array &$form, FormStateInterfac
     $elements['#errors'] = $form_state->getError($elements);
   }
 
-  /**
-   * Wraps drupal_set_message().
-   *
-   * @codeCoverageIgnore
-   */
-  protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
-    drupal_set_message($message, $type, $repeat);
-  }
-
 }
diff --git a/core/lib/Drupal/Core/Form/form.api.php b/core/lib/Drupal/Core/Form/form.api.php
index 554e2710d7..4154412eed 100644
--- a/core/lib/Drupal/Core/Form/form.api.php
+++ b/core/lib/Drupal/Core/Form/form.api.php
@@ -121,7 +121,7 @@ function callback_batch_finished($success, $results, $operations) {
       '#items' => $results,
     ];
     $message .= drupal_render($list);
-    drupal_set_message($message);
+    \Drupal::messenger()->addStatus($message);
   }
   else {
     // An error occurred.
@@ -131,7 +131,7 @@ function callback_batch_finished($success, $results, $operations) {
       '%error_operation' => $error_operation[0],
       '@arguments' => print_r($error_operation[1], TRUE)
     ]);
-    drupal_set_message($message, 'error');
+    \Drupal::messenger()->addError($message);
   }
 }
 
diff --git a/core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php b/core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php
index 075c982773..ab2169d0e8 100644
--- a/core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php
+++ b/core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php
@@ -6,6 +6,7 @@
 use Drupal\Core\Form\ConfigFormBase;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Locale\CountryManagerInterface;
+use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\State\StateInterface;
 use Drupal\user\UserStorageInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -75,13 +76,14 @@ class SiteConfigureForm extends ConfigFormBase {
    * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
    *   The country manager.
    */
-  public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
+  public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager, MessengerInterface $messenger) {
     $this->root = $root;
     $this->sitePath = $site_path;
     $this->userStorage = $user_storage;
     $this->state = $state;
     $this->moduleInstaller = $module_installer;
     $this->countryManager = $country_manager;
+    $this->messenger = $messenger;
   }
 
   /**
@@ -94,7 +96,8 @@ public static function create(ContainerInterface $container) {
       $container->get('entity.manager')->getStorage('user'),
       $container->get('state'),
       $container->get('module_installer'),
-      $container->get('country_manager')
+      $container->get('country_manager'),
+      $container->get('messenger')
     );
   }
 
@@ -134,7 +137,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
     // successfully.)
     $post_params = $this->getRequest()->request->all();
     if (empty($post_params) && (!drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
-      drupal_set_message(t('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href=":handbook_url">online handbook</a>.', ['%dir' => $settings_dir, '%file' => $settings_file, ':handbook_url' => 'https://www.drupal.org/server-permissions']), 'warning');
+      $this->messenger()->addWarning(t('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href=":handbook_url">online handbook</a>.', ['%dir' => $settings_dir, '%file' => $settings_file, ':handbook_url' => 'https://www.drupal.org/server-permissions']));
     }
 
     $form['#attached']['library'][] = 'system/drupal.system';
diff --git a/core/lib/Drupal/Core/Mail/MailManager.php b/core/lib/Drupal/Core/Mail/MailManager.php
index 348dceeb20..3a7a93f8b1 100644
--- a/core/lib/Drupal/Core/Mail/MailManager.php
+++ b/core/lib/Drupal/Core/Mail/MailManager.php
@@ -5,6 +5,7 @@
 use Drupal\Component\Render\PlainTextOutput;
 use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\Plugin\DefaultPluginManager;
 use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
@@ -23,6 +24,7 @@
  */
 class MailManager extends DefaultPluginManager implements MailManagerInterface {
 
+  use MessengerTrait;
   use StringTranslationTrait;
 
   /**
@@ -303,7 +305,7 @@ public function doMail($module, $key, $to, $langcode, $params = [], $reply = NUL
             '%to' => $message['to'],
             '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'),
           ]);
-          drupal_set_message($this->t('Unable to send email. Contact the site administrator if the problem persists.'), 'error');
+          $this->messenger()->addError($this->t('Unable to send email. Contact the site administrator if the problem persists.'));
         }
       }
     }
diff --git a/core/lib/Drupal/Core/Messenger/MessengerTrait.php b/core/lib/Drupal/Core/Messenger/MessengerTrait.php
new file mode 100644
index 0000000000..1454243b8f
--- /dev/null
+++ b/core/lib/Drupal/Core/Messenger/MessengerTrait.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\Core\Messenger;
+
+/**
+ * Provides a trait for the messenger service.
+ */
+trait MessengerTrait {
+
+  /**
+   * The messenger.
+   *
+   * @var \Drupal\Core\Messenger\MessengerInterface
+   */
+  protected $messenger;
+
+  /**
+   * Sets the messenger.
+   *
+   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
+   *   The messenger.
+   */
+  public function setMessenger(MessengerInterface $messenger) {
+    $this->messenger = $messenger;
+  }
+
+  /**
+   * Gets the messenger.
+   *
+   * @return \Drupal\Core\Messenger\MessengerInterface
+   *   The messenger.
+   */
+  public function messenger() {
+    if (!isset($this->messenger)) {
+      $this->messenger = \Drupal::messenger();
+    }
+    return $this->messenger;
+  }
+
+}
diff --git a/core/lib/Drupal/Core/Plugin/PluginBase.php b/core/lib/Drupal/Core/Plugin/PluginBase.php
index 1148768e60..5407dc09b5 100644
--- a/core/lib/Drupal/Core/Plugin/PluginBase.php
+++ b/core/lib/Drupal/Core/Plugin/PluginBase.php
@@ -3,6 +3,7 @@
 namespace Drupal\Core\Plugin;
 
 use Drupal\Component\Plugin\PluginBase as ComponentPluginBase;
+use Drupal\Core\Messenger\MessengerTrait;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
 
@@ -14,5 +15,6 @@
 abstract class PluginBase extends ComponentPluginBase {
   use StringTranslationTrait;
   use DependencySerializationTrait;
+  use MessengerTrait;
 
 }
diff --git a/core/lib/Drupal/Core/Render/Element/StatusMessages.php b/core/lib/Drupal/Core/Render/Element/StatusMessages.php
index 530457fd90..8d84eae607 100644
--- a/core/lib/Drupal/Core/Render/Element/StatusMessages.php
+++ b/core/lib/Drupal/Core/Render/Element/StatusMessages.php
@@ -5,7 +5,7 @@
 /**
  * Provides a messages element.
  *
- * Used to display results of drupal_set_message() calls.
+ * Used to display results of \Drupal::messenger()->addMessage() calls.
  *
  * Usage example:
  * @code
@@ -74,7 +74,13 @@ public static function generatePlaceholder(array $element) {
    */
   public static function renderMessages($type) {
     $render = [];
-    $messages = drupal_get_messages($type);
+    if (isset($type)) {
+      $messages = \Drupal::messenger()->deleteByType($type);
+    }
+    else {
+      $messages = \Drupal::messenger()->deleteAll();
+    }
+
     if ($messages) {
       // Render the messages.
       $render = [
diff --git a/core/tests/Drupal/KernelTests/Core/File/NameMungingTest.php b/core/tests/Drupal/KernelTests/Core/File/NameMungingTest.php
index 2e5d09473a..3cf351b031 100644
--- a/core/tests/Drupal/KernelTests/Core/File/NameMungingTest.php
+++ b/core/tests/Drupal/KernelTests/Core/File/NameMungingTest.php
@@ -38,7 +38,8 @@ public function testMunging() {
     // Disable insecure uploads.
     $this->config('system.file')->set('allow_insecure_uploads', 0)->save();
     $munged_name = file_munge_filename($this->name, '', TRUE);
-    $messages = drupal_get_messages();
+    $messages = \Drupal::messenger()->all();
+    \Drupal::messenger()->deleteAll();
     $this->assertTrue(in_array(strtr('For security reasons, your upload has been renamed to <em class="placeholder">%filename</em>.', ['%filename' => $munged_name]), $messages['status']), 'Alert properly set when a file is renamed.');
     $this->assertNotEqual($munged_name, $this->name, format_string('The new filename (%munged) has been modified from the original (%original)', ['%munged' => $munged_name, '%original' => $this->name]));
   }
diff --git a/core/tests/Drupal/KernelTests/Core/Form/FormValidationMessageOrderTest.php b/core/tests/Drupal/KernelTests/Core/Form/FormValidationMessageOrderTest.php
index 1d20340819..4e056cdd8b 100644
--- a/core/tests/Drupal/KernelTests/Core/Form/FormValidationMessageOrderTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Form/FormValidationMessageOrderTest.php
@@ -80,7 +80,8 @@ public function testLimitValidationErrors() {
     $form_builder = $this->container->get('form_builder');
     $form_builder->submitForm($this, $form_state);
 
-    $messages = drupal_get_messages();
+    $messages = \Drupal::messenger()->all();
+    \Drupal::messenger()->deleteAll();
     $this->assertTrue(isset($messages['error']));
     $error_messages = $messages['error'];
     $this->assertEqual($error_messages[0], 'Three field is required.');
diff --git a/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php b/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php
index 278ec06416..7725c7894f 100644
--- a/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php
+++ b/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php
@@ -8,6 +8,7 @@
 use Drupal\Core\Form\FormAjaxException;
 use Drupal\Core\Form\FormBuilderInterface;
 use Drupal\Core\Form\FormState;
+use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Tests\UnitTestCase;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
@@ -43,6 +44,13 @@ class FormAjaxSubscriberTest extends UnitTestCase {
    */
   protected $stringTranslation;
 
+  /**
+   * The mocked messenger.
+   *
+   * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $messenger;
+
   /**
    * {@inheritdoc}
    */
@@ -52,7 +60,8 @@ protected function setUp() {
     $this->httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
     $this->formAjaxResponseBuilder = $this->getMock('Drupal\Core\Form\FormAjaxResponseBuilderInterface');
     $this->stringTranslation = $this->getStringTranslationStub();
-    $this->subscriber = new FormAjaxSubscriber($this->formAjaxResponseBuilder, $this->stringTranslation);
+    $this->messenger = $this->createMock(MessengerInterface::class);
+    $this->subscriber = new FormAjaxSubscriber($this->formAjaxResponseBuilder, $this->stringTranslation, $this->messenger);
   }
 
   /**
@@ -145,13 +154,19 @@ public function testOnExceptionResponseBuilderException() {
   public function testOnExceptionBrokenPostRequest() {
     $this->formAjaxResponseBuilder->expects($this->never())
       ->method('buildResponse');
+
+    $this->messenger->expects($this->once())
+      ->method('addError');
+
     $this->subscriber = $this->getMockBuilder('\Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber')
-      ->setConstructorArgs([$this->formAjaxResponseBuilder, $this->getStringTranslationStub()])
-      ->setMethods(['drupalSetMessage', 'formatSize'])
+      ->setConstructorArgs([
+        $this->formAjaxResponseBuilder,
+        $this->getStringTranslationStub(),
+        $this->messenger,
+      ])
+      ->setMethods(['formatSize'])
       ->getMock();
-    $this->subscriber->expects($this->once())
-      ->method('drupalSetMessage')
-      ->willReturn('asdf');
+
     $this->subscriber->expects($this->once())
       ->method('formatSize')
       ->with(32 * 1e6)
diff --git a/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php b/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php
index 8e2fc0d2b7..39d2cf846f 100644
--- a/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php
+++ b/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php
@@ -2,7 +2,9 @@
 
 namespace Drupal\Tests\Core\Form;
 
+use Drupal\Core\Form\FormErrorHandler;
 use Drupal\Core\Form\FormState;
+use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Tests\UnitTestCase;
 
 /**
@@ -16,28 +18,31 @@ class FormErrorHandlerTest extends UnitTestCase {
    * @covers ::displayErrorMessages
    */
   public function testDisplayErrorMessages() {
-    $form_error_handler = $this->getMockBuilder('Drupal\Core\Form\FormErrorHandler')
-      ->setMethods(['drupalSetMessage'])
-      ->getMock();
 
-    $form_error_handler->expects($this->at(0))
-      ->method('drupalSetMessage')
-      ->with('invalid', 'error');
-    $form_error_handler->expects($this->at(1))
-      ->method('drupalSetMessage')
-      ->with('invalid', 'error');
-    $form_error_handler->expects($this->at(2))
-      ->method('drupalSetMessage')
-      ->with('invalid', 'error');
-    $form_error_handler->expects($this->at(3))
-      ->method('drupalSetMessage')
-      ->with('no title given', 'error');
-    $form_error_handler->expects($this->at(4))
-      ->method('drupalSetMessage')
-      ->with('element is invisible', 'error');
-    $form_error_handler->expects($this->at(5))
-      ->method('drupalSetMessage')
-      ->with('this missing element is invalid', 'error');
+    /** @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject $messenger */
+    $messenger = $this->createMock(MessengerInterface::class);
+
+    $form_error_handler = new FormErrorHandler();
+    $form_error_handler->setMessenger($messenger);
+
+    $messenger->expects($this->at(0))
+      ->method('addError')
+      ->with('invalid');
+    $messenger->expects($this->at(1))
+      ->method('addError')
+      ->with('invalid');
+    $messenger->expects($this->at(2))
+      ->method('addError')
+      ->with('invalid');
+    $messenger->expects($this->at(3))
+      ->method('addError')
+      ->with('no title given');
+    $messenger->expects($this->at(4))
+      ->method('addError')
+      ->with('element is invisible');
+    $messenger->expects($this->at(5))
+      ->method('addError')
+      ->with('this missing element is invalid');
 
     $form = [
       '#parents' => [],
@@ -97,9 +102,11 @@ public function testDisplayErrorMessages() {
    * @covers ::setElementErrorsFromFormState
    */
   public function testSetElementErrorsFromFormState() {
-    $form_error_handler = $this->getMockBuilder('Drupal\Core\Form\FormErrorHandler')
-      ->setMethods(['drupalSetMessage'])
-      ->getMock();
+    /** @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject $messenger */
+    $messenger = $this->createMock(MessengerInterface::class);
+
+    $form_error_handler = new FormErrorHandler();
+    $form_error_handler->setMessenger($messenger);
 
     $form = [
       '#parents' => [],
