如何在没有任何测试框架的情况下使用嵌入式Cassandra

时间:2019-05-17 07:25:02

标签: cassandra-3.0

我有一个play/scala应用程序,并使用了cassandra数据库。我读到有关embedded-cassandra的内容,并正在尝试使用它。我的应用程序未使用任何junit之类的测试框架(如果可能的话,我会尽量避免使用它们)。

到目前为止,我已经创建了一个工厂和一个cqlstatement。但是我不知道如何执行该语句。在引用Wiki时,它引用了TestCassandra,但是我的IDE找不到此类。我需要使用TestNG还是Junit4

class UsersRepositorySpecs extends PlaySpec /*with BeforeAndAfterAll with BeforeAndAfterEach with OneAppPerSuiteWithComponents*/{
  "UsersRepository Specs" should {
    "create keyspace" in {
      val factory = new LocalCassandraFactory
      println(s"factory is ${factory}")
      factory.setVersion(("3.11.1")) 
      val statement = new CqlStatements(
        """
          |CREATE KEYSPACE myspace
          |  WITH REPLICATION = {
          |   'class' : 'SimpleStrategy',
          |   'replication_factor' : 1
          |  };
        """.stripMargin)

      val cassandra = factory.create
      try {
        cassandra.start()
        val settings = cassandra.getSettings
        println(s"settings are ${settings}")
//HOW DO I EXECUTE THE STATEMENT ?
      } finally cassandra.stop()

    }
  }

}

1 个答案:

答案 0 :(得分:0)

您使用的是com.github.nosan:embedded-cassandra-test还是com.github.nosan:embedded-cassandra?您应该使用第一个。

libraryDependencies += "com.github.nosan" % "embedded-cassandra-test" % "2.0.1" % Test

com.github.nosan:embedded-cassandra-test模块包含TestCassandra类。

LocalCassandraFactory cassandraFactory = new LocalCassandraFactory();
        cassandraFactory.setVersion("3.11.1");

        TestCassandra testCassandra = new TestCassandra(cassandraFactory, CqlScript.statements("raw-statements"),
                CqlScript.classpath("file scripts"));

        try {
            testCassandra.start();

            //your tests
        }
        finally {
            testCassandra.stop();
        }

如果TestCassandra类仍然有问题,可以使用以下示例:


        LocalCassandraFactory cassandraFactory = new LocalCassandraFactory();
        cassandraFactory.setVersion("3.11.1");
        Cassandra cassandra = cassandraFactory.create();
        try {

            cassandra.start();

            Settings settings = cassandra.getSettings();

            //com.datastax.cassandra:cassandra-driver-core:3.7.1
            SocketOptions socketOptions = new SocketOptions();
            socketOptions.setConnectTimeoutMillis(30000);
            socketOptions.setReadTimeoutMillis(30000);
            try (Cluster cluster = Cluster.builder()
                    .addContactPoints(settings.getAddress())
                    .withPort(settings.getPort())
                    .withSocketOptions(socketOptions)
                    .withoutJMXReporting()
                    .withoutMetrics()
                    .build()) {
                Session session = cluster.connect();
                List<String> statements = CqlScript.classpath("schema.cql").getStatements();
                statements.forEach(session::execute);
            }

            //com.datastax.oss:java-driver-core:4.0.1

            DriverConfigLoader driverConfigLoader = DriverConfigLoader.programmaticBuilder()
                    .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30))
                    .withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(3))
                    .build();

            try (CqlSession cqlSession = CqlSession.builder().addContactPoint(
                    new InetSocketAddress(settings.getAddress(), settings.getPort()))
                    .withLocalDatacenter("datacenter1")
                    .withConfigLoader(driverConfigLoader)
                    .build()) {

                List<String> statements = CqlScript.classpath("schema.cql").getStatements();
                statements.forEach(cqlSession::execute);
            }

        }
        finally {
            cassandra.stop();
        }