WebTau db. module can operate on JVM DataSource without providing JDBC URLs. In the following sections, we will cover https://spring.io/projects/spring-boot Spring Boot Repository Test.We need to configure WebTau db. module as well as Spring test one @DataJpaTest @ActiveProfiles("tc") // test profile with Test Containers public class AccountRepositoryTest { private final AccountRepository accountRepository; private final Database mainDb; private final DatabaseTable ACCOUNT; @Autowired public AccountRepositoryTest(DataSource dataSource, AccountRepository accountRepository) { this.accountRepository = accountRepository; mainDb = db.labeled("main-db").fromDataSource(dataSource); // define WebTau database instance to insert/read ACCOUNT = mainDb.table("ACCOUNT"); // define ACCOUNT table instance to insert/read } Here is how we define tc profile using https://www.testcontainers.org/modules/databases/jdbc/ Test Containers JDBC support: # test containers special JDBC url spring.datasource.url=jdbc:tc:postgresql:9.6.8:///my-db spring.datasource.driverClassName=org.testcontainers.jdbc.ContainerDatabaseDriver spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect spring.jpa.hibernate.ddl-auto=create-drop # disable default in memory DB provided by @DataJpaTest spring.test.database.replace=none
Let's write data directly to database table, bypassing repository class, so we can isolate our test logic. @Test public void findById() { // define data to be inserted into DB TableData newAccounts = table( "ID", "FIRST_NAME", "LAST_NAME", ________________________________, "id1", "FN1" , "LN1", "id2", "FN2" , "LN2"); // insert directly to DB bypassing repository ACCOUNT.insert(newAccounts); // use accounts repository to fetch a record Account account = accountRepository.findById("id2").get(); // use bean and map comparison shortcut actual(account).should(equal(map( "id", "id2", "firstName", "FN2", "lastName", "LN2"))); }
Now let's write data using repository and validate that DB table actually contains the data. We then will test a simple query by last name. @Test public void createEntriesAndFindByName() { TableData newAccounts = table( "*id", "firstName" , "lastName", ________________________________, "id1", "FN1" , "LN", "id2", "FN2" , "LN", "id3", "FNN1" , "LNN"); List<Account> accounts = createAccounts(newAccounts); // create accounts java beans from table data accountRepository.saveAll(accounts); // force data commit to DB TestTransaction.flagForCommit(); TestTransaction.end(); // query all data from DB, using * to note that we depend on ID for compare and not order ACCOUNT.query().should(equal(table("*ID", "FIRST_NAME", "LAST_NAME", ________________________________, "id1", "FN1" , "LN", "id2", "FN2" , "LN", "id3", "FNN1" , "LNN"))); // WebTau will automatically convert actual column names from underscores to camelCase based on expected column names ACCOUNT.query().should(equal(newAccounts)); // search by last name and validate received java beans List<Account> lnAccounts = accountRepository.findByLastName("LN"); actual(lnAccounts).should(equal(table("*id", "firstName", "lastName", ______________________________, "id1", "FN1" , "LN", "id2", "FN2" , "LN"))); } private static List<Account> createAccounts(TableData tableData) { return tableData.rowsStream().map(row -> { Account account = new Account(); account.setId(row.get("id")); account.setFirstName(row.get("firstName")); account.setLastName(row.get("lastName")); return account; }).collect(Collectors.toList()); }
In the test above we repeated test data with only difference in column names. In setup, we used camelCase, and in expectations underscores.WebTau automatically converts data from one format to another depending on the context. I.e. if you compare table query result with FIRST_NAME as a column against a TableData with firstName column, WebTau will convert actual before comparison to match expected column names format. @Test public void findByIdReuseData() { // using camelCase for properties TableData newAccounts = table( "*id", "firstName" , "lastName", ________________________________, "id1", "FN1" , "LN1", "id2", "FN2" , "LN2"); // WebTau will automatically convert camelCase to underscores at insert time ACCOUNT.insert(newAccounts); // use accounts repository to fetch a record Account account = accountRepository.findById("id2").get(); // reuse row from newAccounts table as expected value actual(account).should(equal(newAccounts.findByKey("id2"))); } @Test public void createEntriesAndFindByNameReuseData() { TableData newAccounts = table("*id", "firstName", "lastName", ______________________________, "id1", "FN1" , "LN", "id2", "FN2" , "LN", "id3", "FNN1" , "LNN"); List<Account> accounts = createAccounts(newAccounts); // create accounts java beans from table data accountRepository.saveAll(accounts); // force data commit to DB TestTransaction.flagForCommit(); TestTransaction.end(); // another way to compare. // WebTau will automatically convert actual column names from underscores to camelCase based on expected column names format ACCOUNT.query().should(equal(newAccounts)); // search by last name and validate received java beans List<Account> lnAccounts = accountRepository.findByLastName("LN"); TableData expectedByLn = newAccounts.fromRowsByKeys("id1", "id2"); actual(lnAccounts).should(equal(expectedByLn)); }
In between tests we will explicitly delete data from DB @BeforeEach public void cleanupBeforeTest() { ACCOUNT.clear(); }
import static org.testingisdocumenting.webtau.db.Database.db; To include only Database module as your dependency use <dependency> <groupId>org.testingisdocumenting.webtau</groupId> <artifactId>webtau-data</artifactId> <version>2.4</version> </dependency>