diff --git a/core/modules/user/src/Tests/UserSaveTest.php b/core/modules/user/src/Tests/UserSaveTest.php
index fdd468c..a2eaf5d 100644
--- a/core/modules/user/src/Tests/UserSaveTest.php
+++ b/core/modules/user/src/Tests/UserSaveTest.php
@@ -21,7 +21,12 @@ class UserSaveTest extends WebTestBase {
    */
   function testUserImport() {
     // User ID must be a number that is not in the database.
-    $max_uid = db_query('SELECT MAX(uid) FROM {users}')->fetchField();
+
+    $uids = \Drupal::entityManager()->getStorage('user')->getQuery()
+      ->sort('uid', 'DESC')
+      ->range(0, 1)
+      ->execute();
+    $max_uid = reset($uids);
     $test_uid = $max_uid + mt_rand(1000, 1000000);
     $test_name = $this->randomMachineName();
 
