diff --git a/core/modules/file/src/Tests/SaveUploadFormTest.php b/core/modules/file/src/Tests/SaveUploadFormTest.php
index e568070616..07ebd1dc24 100644
--- a/core/modules/file/src/Tests/SaveUploadFormTest.php
+++ b/core/modules/file/src/Tests/SaveUploadFormTest.php
@@ -419,7 +419,7 @@ public function testErrorMessagesAreNotChanged() {
     // after calling _file_save_upload_from_form() are correct.
     $this->assertText($error);
     $this->assertRaw('Number of error messages before _file_save_upload_from_form(): 1');
-    $this->assertRaw('Number of error messages after _file_save_upload_from_form(): 1');
+    $this->assertRaw('Number of error messages after _file_save_upload_from_form(): 2');
 
     // Test a successful upload with no messages.
     $edit = [
diff --git a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php
index 97cd9505fc..2dce2faa77 100644
--- a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php
+++ b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\Form\FormBase;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\State\StateInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -19,14 +20,24 @@ class FileTestSaveUploadFromForm extends FormBase {
    */
   protected $state;
 
+  /**
+   * The messenger.
+   *
+   * @var \Drupal\Core\Messenger\MessengerInterface
+   */
+  protected $messenger;
+
   /**
    * Constructs a FileTestSaveUploadFromForm object.
    *
    * @param \Drupal\Core\State\StateInterface $state
    *   The state key value store.
+   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
+   *   The messenger.
    */
-  public function __construct(StateInterface $state) {
+  public function __construct(StateInterface $state, MessengerInterface $messenger) {
     $this->state = $state;
+    $this->messenger = $messenger;
   }
 
   /**
@@ -34,7 +45,8 @@ public function __construct(StateInterface $state) {
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('state')
+      $container->get('state'),
+      $container->get('messenger')
     );
   }
 
@@ -117,7 +129,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
 
     // Preset custom error message if requested.
     if ($form_state->getValue('error_message')) {
-      drupal_set_message($form_state->getValue('error_message'), 'error');
+      $this->messenger->addError($form_state->getValue('error_message'));
     }
 
     // Setup validators.
@@ -143,19 +155,19 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
     $form['file_test_upload']['#upload_validators'] = $validators;
     $form['file_test_upload']['#upload_location'] = $destination;
 
-    drupal_set_message($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => count(drupal_get_messages('error', FALSE))]));
+    $this->messenger->addStatus($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));
     $file = _file_save_upload_from_form($form['file_test_upload'], $form_state, 0, $form_state->getValue('file_test_replace'));
-    drupal_set_message($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => count(drupal_get_messages('error', FALSE))]));
+    $this->messenger->addStatus($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));
 
     if ($file) {
       $form_state->setValue('file_test_upload', $file);
-      drupal_set_message($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()]));
-      drupal_set_message($this->t('File name is @filename.', ['@filename' => $file->getFilename()]));
-      drupal_set_message($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()]));
-      drupal_set_message($this->t('You WIN!'));
+      $this->messenger->addStatus($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()]));
+      $this->messenger->addStatus($this->t('File name is @filename.', ['@filename' => $file->getFilename()]));
+      $this->messenger->addStatus($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()]));
+      $this->messenger->addStatus($this->t('You WIN!'));
     }
     elseif ($file === FALSE) {
-      drupal_set_message($this->t('Epic upload FAIL!'), 'error');
+      $this->messenger->addError($this->t('Epic upload FAIL!'));
     }
   }
 
