More Related Content Similar to Take Control of your Integration Testing with TestContainers (20) Take Control of your Integration Testing with TestContainers1. Take Control of your
Integration Testing with
TestContainers
Naresha K
@naresha_k
https://blog.nareshak.com/
15. @Testcontainers
public class SampleMySqlTest {
@Container
private MySQLContainer mySQLContainer = new
MySQLContainer(DockerImageName.parse("mysql:8"))
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
@Test
void test1() {
// invoke SUT && assert
assertTrue(mySQLContainer.isRunning());
}
@Test
void test2() {
// invoke SUT && assert
assertTrue(mySQLContainer.isRunning());
}
}
16. @Testcontainers
public class SampleMySqlTest {
@Container
private MySQLContainer mySQLContainer = new
MySQLContainer(DockerImageName.parse("mysql:8"))
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
@Test
void test1() {
// invoke SUT && assert
assertTrue(mySQLContainer.isRunning());
}
@Test
void test2() {
// invoke SUT && assert
assertTrue(mySQLContainer.isRunning());
}
}
@Testcontainers
@Container
17. @Testcontainers
public class SampleMySqlTest {
@Container
private static MySQLContainer mySQLContainer = new
MySQLContainer(DockerImageName.parse("mysql:8"))
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
@Test
void test1() {
// invoke SUT && assert
assertTrue(mySQLContainer.isRunning());
}
@Test
void test2() {
// invoke SUT && assert
assertTrue(mySQLContainer.isRunning());
}
}
18. @Testcontainers
public class MySqlTest {
@Container
protected static final MySQLContainer mySQLContainer;
static {
mySQLContainer = new
MySQLContainer(DockerImageName.parse("mysql:8"))
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
mySQLContainer.start();
}
}
22. select
this_.id as id1_0_0_,
this_.version as version2_0_0_,
this_.name as name3_0_0_,
this_.city as city4_0_0_
from conference this_
where this_.city in ()
23. hibernate:
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
dataSource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password: ''
logSql: true
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
Application.yml (Test with H2)
25. dataSource:
pooled: true
jmxExport: true
driverClassName: com.mysql.jdbc.Driver
username: root
password: secret
logSql: true
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:mysql://localhost:3306/testapp
Application.yml (Test with MySQL)
30. void "should return empty list"() {
given:
List<String> cities = []
when:
List<Conference> conferences =
conferenceService.getConferencesInCities(cities)
then:
conferences.size() == 0
}
32. @Integration
@Rollback
@Testcontainers
class ConferenceTestContainerSpec extends Specification {
@Shared
MySQLContainer mySQLContainer = new MySQLContainer()
.withDatabaseName("testapp")
.withUsername("user")
.withPassword("secret")
ConferenceService conferenceService
void "should return empty list"() {
// test code
}
}
dataSource:
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
environments:
test:
dataSource:
dbCreate: update
url: jdbc:tc:mysql:8://somehostname:someport/databasename
35. @Integration
@Testcontainers
class FileStorageSpec extends Specification {
public static final String TARGET_BUCKET = "testbucket"
AmazonS3Service amazonS3Service
FileStorageService fileStorageService
@Shared
public LocalStackContainer localstack = new LocalStackContainer(
DockerImageName.parse("localstack/localstack:0.11.2"))
.withServices(S3)
36. void setup() {
AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(
localstack.getEndpointConfiguration(S3))
.withCredentials(
localstack.getDefaultCredentialsProvider())
.build();
amazonS3Service.client = s3
amazonS3Service.createBucket(TARGET_BUCKET)
}
37. def "file can be stored in s3 storage and read"() {
given:
def filePath = Paths.get("src/test/resources/car.jpg")
InputStream stream = Files.newInputStream(filePath)
when:
fileStorageService.storeFile(TARGET_BUCKET, "vehicles/123.jpg", stream)
then:
amazonS3Service.exists(TARGET_BUCKET, "vehicles/123.jpg")
when:
File file = fileStorageService.readFile(TARGET_BUCKET, "vehicles/123.jpg",
Files.createTempFile(null, null).toAbsolutePath().toString())
then:
file
and:
file.newInputStream().bytes == filePath.bytes
}
40. @Testcontainers
public class GenericContainerTest {
@Container
private static GenericContainer<?> redis =
new GenericContainer<>(
DockerImageName.parse("redis:6"))
.withExposedPorts(6379);
@Test
void testRedisOps() {
assertTrue(redis.isRunning());
}
}
41. @Testcontainers
public class GenericContainerTest {
@Container
private static GenericContainer<?> redis =
new GenericContainer<>(
DockerImageName.parse("redis:6"))
.withExposedPorts(6379);
@Test
void testRedisOps() {
assertTrue(redis.isRunning());
}
}
//get host port
redis.getMappedPort(6379)
45. @Container
private static GenericContainer<?> tomcat = new
GenericContainer<>(
DockerImageName.parse("tomcat:8.5.8-jre8"))
.withExposedPorts(8080)
.waitingFor(Wait.forHttp("/")
.forStatusCode(200));
https://www.testcontainers.org/features/startup_and_waits/