how to use embedded-cassandra without any test framework

删除回忆录丶 提交于 2019-12-11 19:28:44

问题


I have a play/scala application and uses cassandra database. I read about embedded-cassandra and am trying to use it. My application doesn't use any test frameworks like junit (I'll prefer not to use them to avoid if possible).

So far, I have created a factory and a cqlstatement. But I can't figure out how to execute the statement. Referring to the wiki, it refers to TestCassandra but my IDE can't find this class. Do I need to use TestNG or 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:


Which dependency are you using com.github.nosan:embedded-cassandra-test or com.github.nosan:embedded-cassandra ? You should use the first one.

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

com.github.nosan:embedded-cassandra-test module contains TestCassandra class.

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();
        }

If you still have an issue with TestCassandra class, you can use the following examples:


        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();
        }



来源:https://stackoverflow.com/questions/56181569/how-to-use-embedded-cassandra-without-any-test-framework

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!