We spend hours on Instagram and YouTube and waste cash on espresso and quick meals, however received’t spend half-hour a day studying expertise to spice up our careers.
Grasp in DevOps, SRE, DevSecOps & MLOps!
Study from Guru Rajesh Kumar and double your wage in only one 12 months.

AssertJ-DB is an extension of the AssertJ assertion library designed particularly for database testing in Java.
It allows builders and testers to put in writing fluent, readable, and chainable assertions immediately towards relational database content material like tables, requests (queries), and modifications (insert/replace/delete operations).
In contrast to conventional database verification strategies that rely closely on handbook SQL queries and sophisticated checks, AssertJ-DB means that you can check database states declaratively, in plain Java, enhancing check maintainability, readability, and developer productiveness.
AssertJ-DB works with JDBC-compatible databases resembling MySQL, PostgreSQL, Oracle, SQL Server, H2, and extra.
It’s significantly worthwhile in unit checks, integration checks, and end-to-end testing the place validating the database is a vital side.
- Database State Validation
Affirm that after performing operations (e.g., creating an order, updating a profile), the database displays the anticipated information. - Change Monitoring Exams
Validate {that a} particular quantity and sort of database modifications (insert, replace, delete) happen after an motion. - SQL Question Outcome Assertions
Check particular queries or saved procedures by asserting the construction and information of the returned consequence set. - Transactional Testing
Assert the state of tables throughout or after transactions with out writing uncooked JDBC code. - Knowledge Migration Verification
When migrating information from one schema/model/system to a different, assert that the ensuing tables match anticipated situations. - CI/CD Pipeline Testing
Combine AssertJ-DB checks into automated pipelines to make sure database consistency after deployments.

Core Elements:
- Desk:
Represents the total content material of a selected database desk at a time limit. - Request:
Represents the results of executing a customized SQL question. - Modifications:
Captures and asserts variations in database state over time (e.g., between two operations). - Assertions API:
Fluent Java assertions for columns, rows, values, and modifications.
How It Works:
- Connection Setup
AssertJ-DB connects to the database utilizing JDBC. - Snapshot
It captures snapshots of desk states or question outcomes. - Comparability & Assertions
Builders write fluent assertions evaluating anticipated values with precise database contents. - Change Monitoring
Optionally, builders can seize and examine database state earlier than and after actions to claim modifications.
+------------------+
| JDBC Connection |
+--------+---------+
|
v
+------------------------+
| Desk / Request / Modifications |
+------------------------+
|
v
+----------------------------+
| AssertJ-DB Fluent Assertions |
+----------------------------+
|
v
+---------------------+
| Check Go / Fail |
+---------------------+
- Connect with Database
Use JDBC URL, consumer, and password to arrange the connection. - Take a Snapshot
Seize the present state of a desk, a customized SQL question, or database modifications. - Carry out Actions
Set off the operations you wish to check (e.g., insert information, replace information). - Seize Modifications (Non-obligatory)
Seize new database state if change monitoring is required. - Write Assertions
Use AssertJ-DB fluent API to claim database values, rows, columns, or modifications. - Run Exams
Execute checks in your JUnit/TestNG testing framework.
✅ Pre-requisites
- Java 8 or greater
- JUnit 5 or TestNG put in
- Maven or Gradle construct system
- A working database occasion (MySQL, PostgreSQL, H2, and so on.)
🛠️ Step 1: Add AssertJ-DB to Your Challenge
For Maven:
org.assertj
assertj-db
2.0.2
check
For Gradle:
testImplementation 'org.assertj:assertj-db:2.0.2'
🛠️ Step 2: Setup JDBC Connection
Connection connection = DriverManager.getConnection(
"jdbc:h2:mem:testdb", "sa", ""
);
(Change together with your database URL, username, password.)
🛠️ Step 3: Create a Desk Assertion
Instance: Validate Desk Content material
import org.assertj.db.sort.Desk;
import static org.assertj.db.api.Assertions.assertThat;
Desk desk = new Desk(connection, "customers");
assertThat(desk)
.hasNumberOfRows(2)
.row(0)
.worth("username").isEqualTo("admin")
.worth("e-mail").isEqualTo("admin@instance.com");
🛠️ Step 4: Observe and Assert Modifications
Instance: Validate Modifications After an Insert
import org.assertj.db.sort.Modifications;
Modifications modifications = new Modifications(connection);
modifications.setStartPointNow();
// Carry out motion: insert a consumer
assertion.executeUpdate("INSERT INTO customers(username, e-mail) VALUES('john', 'john@instance.com')");
modifications.setEndPointNow();
assertThat(modifications)
.hasNumberOfChanges(1)
.change()
.isCreation()
.rowAtEndPoint()
.worth("username").isEqualTo("john")
.worth("e-mail").isEqualTo("john@instance.com");
🛠️ Step 5: Run Exams
Run the above check class as a JUnit 5 or TestNG check.
✅ You now have robust and readable database assertions inside your Java checks!