private List<HRegionLocation> getRegionsInRange(TableName tableName, byte[] startKey,
    byte[] endKey) throws IOException {
    List<HRegionLocation> regionsInRange = new ArrayList<>();
    byte[] currentKey = startKey;
    final boolean endKeyIsEndOfTable = Bytes.equals(endKey, HConstants.EMPTY_END_ROW);
    try (RegionLocator r = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
      do {
        HRegionLocation regionLocation = r.getRegionLocation(currentKey);
        regionsInRange.add(regionLocation);
        currentKey = regionLocation.getRegion().getEndKey();
      } while (
        !Bytes.equals(currentKey, HConstants.EMPTY_END_ROW)
          && (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0)
      );
      return regionsInRange;
    }
  }
Tests getRegionsInRange by creating some regions over which a range of keys spans; then
changing the key range.
hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide5.java