Skip to content

Instantly share code, notes, and snippets.

@alexanderankin
Last active November 6, 2023 09:07
Show Gist options
  • Save alexanderankin/08ce29324cb3a667531d677369705bab to your computer and use it in GitHub Desktop.
Save alexanderankin/08ce29324cb3a667531d677369705bab to your computer and use it in GitHub Desktop.
// org.springframework.boot:spring-boot-starter-data-jdbc
package org.example.p1699205514379359;
import lombok.Data;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
/**
* couldn't use AutoConfigureTestDatabase because it seems like for only embedded db like h2
* <p>
* couldn't use DataJpaTest because we need all of springboot to wake up for this and configure databases,
* unlike DataJpaTest which apparently scopes things such that it doesn't even create the DataSource bean.
*/
@SpringBootTest
@ActiveProfiles("test")
// @DataJpaTest
// @AutoConfigureTestDatabase
@TestPropertySource(properties = {
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///integration-tests-db?testcase=SbTcJdbcUrlExampleTest",
"spring.datasource.hikari.minimum-idle=1",
})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SbTcJdbcUrlExampleTest {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
@Order(1)
void setupDb() {
System.out.println("setupDb in " + getClass().getSimpleName());
jdbcTemplate.execute("create table abc(id serial, name varchar(500) unique)");
jdbcTemplate.execute("insert into abc(name) values('alice')");
}
@Test
@Order(2)
void fetchData() {
System.out.println("fetchData in " + getClass().getSimpleName());
List<Abc> result = jdbcTemplate.query("select * from abc", new BeanPropertyRowMapper<>(Abc.class));
assertThat(result, hasSize(1));
assertThat(result.get(0).getName(), is("alice"));
}
}
@SpringBootTest
@ActiveProfiles("test")
// @DataJpaTest
// @AutoConfigureTestDatabase
@TestPropertySource(properties = {
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///integration-tests-db?testcase=SbTcJdbcUrlExampleTest2",
"spring.datasource.hikari.minimum-idle=1",
})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SbTcJdbcUrlExampleTest2 {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
@Order(1)
void setupDb() {
System.out.println("setupDb in " + getClass().getSimpleName());
jdbcTemplate.execute("create table abc(id serial, name varchar(500) unique)");
jdbcTemplate.execute("insert into abc(name) values('bob')");
jdbcTemplate.execute("insert into abc(name) values('chris')");
}
@Test
@Order(2)
void fetchData() {
System.out.println("fetchData in " + getClass().getSimpleName());
List<Abc> result = jdbcTemplate.query("select * from abc", new BeanPropertyRowMapper<>(Abc.class));
assertThat(result, hasSize(2));
assertThat(result.stream().map(Abc::getName).toList(), containsInAnyOrder("bob", "chris"));
}
}
@SpringBootApplication
class App {
}
@Data
class Abc {
Integer id;
String name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment