@BeforeClass
  public static void setupBeforeClass() throws Exception {
    conf = UTIL.getConfiguration();
    Random rand = ThreadLocalRandom.current();
    for (int i = 0; i < 10; i++) {
      do {
        int sign = i % 2 == 0 ? 1 : -1;
        connectorPort += sign * rand.nextInt(100);
      } while (!HBaseTestingUtil.available(connectorPort));
      try {
        conf.setInt("regionserver.rmi.registry.port", connectorPort);
        cluster = UTIL.startMiniCluster();
        LOG.info("Waiting for active/ready master");
        cluster.waitForActiveAndReadyMaster();
        master = cluster.getMaster();
        break;
      } catch (Exception e) {
        LOG.debug("Encountered exception when starting mini cluster. Trying port " + connectorPort,
          e);
        try {
          // this is to avoid "IllegalStateException: A mini-cluster is already running"
          UTIL.shutdownMiniCluster();
        } catch (Exception ex) {
          LOG.debug("Encountered exception shutting down cluster", ex);
        }
      }
    }
  }
Setup the environment for the test.
hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestBalancerStatusTagInJMXMetrics.java