Skip to content

Instantly share code, notes, and snippets.

@mihaita-tinta
Last active December 22, 2023 14:40
Show Gist options
  • Save mihaita-tinta/be5280e4148508a5bea99d5b4f61c25f to your computer and use it in GitHub Desktop.
Save mihaita-tinta/be5280e4148508a5bea99d5b4f61c25f to your computer and use it in GitHub Desktop.
junit testcontainer support
package com.mih.playground.driver;
import com.datastax.driver.core.utils.UUIDs;
import com.mih.playground.Infrastructure;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.cassandra.DataCassandraTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;
import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import java.time.ZonedDateTime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@DataCassandraTest
@TestPropertySource(properties = "logging.level.org.springframework.data.cassandra.core.cql.CqlTemplate=DEBUG")
@Testcontainers
class DriverRepositoryTestcontainersTest {
@Container
public static CassandraContainer<?> cassandraContainer = (CassandraContainer) new CassandraContainer(DockerImageName.parse("cassandra:latest"))
.withInitScript("schema.cql")
.withExposedPorts(9042)
.withReuse(true);
@BeforeAll
static void setupCassandraConnectionProperties() {
System.setProperty("spring.cassandra.contact-points", "127.0.0.1");
System.setProperty("spring.cassandra.port", String.valueOf(cassandraContainer.getMappedPort(9042)));
}
@Autowired
DriverRepository repository;
@Test
void test() {
Driver driver = new Driver(UUIDs.timeBased(), ZonedDateTime.now().toEpochSecond(), "nickname");
Driver db = repository.save(driver);
assertNotNull(db.getId());
assertEquals(1, repository.findAll().size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment