From 2e8cb2c9062aff83b1566ee2ffb99b1ebd481404 Mon Sep 17 00:00:00 2001 From: yuvalshi0 Date: Sat, 9 Apr 2022 15:37:57 +0300 Subject: [PATCH 1/2] Fixing null null-pointer-bugr --- .../akka/kafka/testkit/internal/TestcontainersKafka.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala b/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala index 49d7f5ba4..b7664145a 100644 --- a/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala +++ b/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala @@ -16,13 +16,17 @@ import scala.jdk.CollectionConverters._ object TestcontainersKafka { trait Spec extends KafkaSpec { - private var cluster: KafkaContainerCluster = _ private var kafkaBootstrapServersInternal: String = _ private var kafkaPortInternal: Int = -1 private def requireStarted(): Unit = require(kafkaPortInternal != -1, "Testcontainers Kafka hasn't been started via `setUp`") + private var cluster: KafkaContainerCluster = { + requireStarted() + null + } + /** * Override this to change default settings for starting the Kafka testcontainers cluster. */ From 9aef1442947c919fa97ab517c1fa78e0270b2328 Mon Sep 17 00:00:00 2001 From: yuvalshi0 Date: Sat, 9 Apr 2022 16:32:15 +0300 Subject: [PATCH 2/2] adding requireStarted to public methods --- .../internal/TestcontainersKafka.scala | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala b/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala index b7664145a..daba7d3ce 100644 --- a/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala +++ b/testkit/src/main/scala/akka/kafka/testkit/internal/TestcontainersKafka.scala @@ -16,16 +16,12 @@ import scala.jdk.CollectionConverters._ object TestcontainersKafka { trait Spec extends KafkaSpec { + private var cluster: KafkaContainerCluster = _ private var kafkaBootstrapServersInternal: String = _ private var kafkaPortInternal: Int = -1 private def requireStarted(): Unit = - require(kafkaPortInternal != -1, "Testcontainers Kafka hasn't been started via `setUp`") - - private var cluster: KafkaContainerCluster = { - requireStarted() - null - } + require(cluster != null || kafkaPortInternal != -1, "Testcontainers Kafka hasn't been started via `setUp`") /** * Override this to change default settings for starting the Kafka testcontainers cluster. @@ -43,18 +39,29 @@ object TestcontainersKafka { kafkaBootstrapServersInternal } - def brokerContainers: Vector[AlpakkaKafkaContainer] = cluster.getBrokers.asScala.toVector + def brokerContainers: Vector[AlpakkaKafkaContainer] = { + requireStarted() + cluster.getBrokers.asScala.toVector + } - def zookeeperContainer: GenericContainer[_] = cluster.getZooKeeper + def zookeeperContainer: GenericContainer[_] = { + requireStarted() + cluster.getZooKeeper + } - def schemaRegistryContainer: Option[SchemaRegistryContainer] = cluster.getSchemaRegistry.asScala + def schemaRegistryContainer: Option[SchemaRegistryContainer] = { + requireStarted() + cluster.getSchemaRegistry.asScala + } - def getSchemaRegistryUrl: String = + def getSchemaRegistryUrl: String = { + requireStarted() cluster.getSchemaRegistry.asScala .map(_.getSchemaRegistryUrl) .getOrElse( throw new RuntimeException("Did you enable schema registry in your KafkaTestkitTestcontainersSettings?") ) + } def startCluster(): String = startCluster(testcontainersSettings)