code: public void stop() {
          if (kafkaServer != null) {
              try {
                  if (kafkaServer.brokerState().currentState() != (NotRunning.state())) {
                      kafkaServer.shutdown();
                      kafkaServer.awaitShutdown();
                  }
              } catch (Exception e) {
                  log.warn("Failed to shutdown Kafka embedded server", e);
              }
  
              try {
                  CoreUtils.delete(kafkaServer.config().logDirs());
              } catch (Exception e) {
                  log.warn("Failed to remove logs on Kafka embedded server", e);
              }
          }
  
          if (serverFactory != null) {
              try {
                  serverFactory.shutdown();
              } catch (Exception e) {
                  log.warn("Failed to shutdown Zookeeper instance", e);
              }
          }
      }
docs: Shutdown embedded Kafka and Zookeeper server instances