Skip to content

Commit 0715858

Browse files
authored
BAEL 8160 - How to tear down / empty database after test in HSQL? (#17786)
* update parent-boot-2 properties * add HSQL DB version * Update pom.xml * add HSQL DB Dependency * Create application-hsql.properties * Add application properties of HSQL DB * Create DbteardownApplication.java * Create Customer.java * create customer Entity * Create CustomerRepository.java * Create CustomerController.java * Create JdbcTemplateLiveTest.java * Add a teardown method which use JdbcTemplate class * Create JdbcTestUtilsLiveTest.java * add tear down method which use JdbcTestUtility Class * Create PropertiesLiveTest.java * add a tear down method which use Properties configuration * Create SqlScriptLiveTest * add a tear down method which use sql script * add a cleanup.sql * Create TransactionalLiveTest.java * add a tear down method which use Transaction * renamed test method names * Update application-hsql.properties * Refactor tests to use repository instead of MVC, add @DirtiesContext to isolate side effects * Switched from MockMvc to repository-based tests to streamline test execution and focus on database operations. * Added @DirtiesContext annotation to ensure test isolation and prevent unintended side effects between tests. * Full test isolation
1 parent f99d434 commit 0715858

File tree

12 files changed

+395
-0
lines changed

12 files changed

+395
-0
lines changed

parent-boot-2/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<mysql-connector-java.version>8.2.0</mysql-connector-java.version>
110110
<org.slf4j.version>1.7.32</org.slf4j.version>
111111
<logback.version>1.2.7</logback.version>
112+
<hsqldb.version>2.7.1</hsqldb.version>
112113
</properties>
113114

114115
</project>

testing-modules/spring-testing-2/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<artifactId>byte-buddy</artifactId>
5151
<version>${byte-buddy.version}</version>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.hsqldb</groupId>
55+
<artifactId>hsqldb</artifactId>
56+
<version>${hsqldb.version}</version>
57+
<scope>test</scope>
58+
</dependency>
5359
</dependencies>
5460

5561
<build>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.dbteardown;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "customers")
11+
public class Customer {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private long id;
16+
17+
private String name;
18+
19+
private String email;
20+
21+
public Customer() {
22+
23+
}
24+
25+
public Customer(String name, String email) {
26+
this.name = name;
27+
this.email = email;
28+
}
29+
30+
public void setId(long id) {
31+
this.id = id;
32+
}
33+
34+
public long getId() {
35+
return id;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setEmail(String email) {
47+
this.email = email;
48+
}
49+
50+
public String getEmail() {
51+
return email;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
57+
}
58+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.dbteardown;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface CustomerRepository extends JpaRepository<Customer, Long> {
10+
11+
Optional<Customer> findByName(String name);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.dbteardown;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DbteardownApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DbteardownApplication.class, args);
11+
}
12+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.dbteardown;
2+
3+
import java.util.Optional;
4+
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.jdbc.core.JdbcTemplate;
12+
import org.springframework.test.annotation.DirtiesContext;
13+
import org.springframework.test.context.ActiveProfiles;
14+
15+
@SpringBootTest
16+
@ActiveProfiles("hsql")
17+
@DirtiesContext
18+
class JdbcTemplateIntegrationTest {
19+
20+
@Autowired
21+
private CustomerRepository customerRepository;
22+
23+
@Autowired
24+
private JdbcTemplate jdbcTemplate;
25+
26+
@BeforeEach
27+
void setUp() {
28+
String insertSql = "INSERT INTO customers (name, email) VALUES (?, ?)";
29+
Customer customer = new Customer("John", "[email protected]");
30+
jdbcTemplate.update(insertSql, customer.getName(), customer.getEmail());
31+
}
32+
33+
@AfterEach
34+
void tearDown() {
35+
this.jdbcTemplate.execute("TRUNCATE TABLE customers");
36+
}
37+
38+
@Test
39+
void givenCustomer_whenSaved_thenReturnSameCustomer() throws Exception {
40+
Customer customer = new Customer("Doe", "[email protected]");
41+
42+
Customer savedCustomer = customerRepository.save(customer);
43+
44+
Assertions.assertEquals(customer, savedCustomer);
45+
}
46+
47+
@Test
48+
void givenExistingCustomer_whenFoundByName_thenReturnCustomer() throws Exception {
49+
Optional<Customer> customer = customerRepository.findByName("John");
50+
51+
Assertions.assertTrue(customer.isPresent(), "Customer should be present");
52+
Assertions.assertEquals("John", customer.get()
53+
.getName(), "Customer name should match");
54+
}
55+
56+
@Test
57+
void givenNonExistingCustomer_whenFoundByName_thenReturnNull() throws Exception {
58+
Optional<Customer> customer = customerRepository.findByName("Doe");
59+
60+
Assertions.assertFalse(customer.isPresent(), "Customer should not be found");
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.dbteardown;
2+
3+
import java.util.Optional;
4+
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.jdbc.core.JdbcTemplate;
12+
import org.springframework.test.annotation.DirtiesContext;
13+
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.jdbc.JdbcTestUtils;
15+
16+
@SpringBootTest
17+
@ActiveProfiles("hsql")
18+
@DirtiesContext
19+
public class JdbcTestUtilsIntegrationTest {
20+
21+
@Autowired
22+
private CustomerRepository customerRepository;
23+
24+
@Autowired
25+
private JdbcTemplate jdbcTemplate;
26+
27+
@BeforeEach
28+
void setUp() {
29+
String insertSql = "INSERT INTO customers (name, email) VALUES (?, ?)";
30+
Customer customer = new Customer("John", "[email protected]");
31+
jdbcTemplate.update(insertSql, customer.getName(), customer.getEmail());
32+
}
33+
34+
@AfterEach
35+
void tearDown() {
36+
JdbcTestUtils.deleteFromTables(jdbcTemplate, "customers");
37+
}
38+
39+
@Test
40+
void givenCustomer_whenSaved_thenReturnSameCustomer() throws Exception {
41+
Customer customer = new Customer("Doe", "[email protected]");
42+
43+
Customer savedCustomer = customerRepository.save(customer);
44+
45+
Assertions.assertEquals(customer, savedCustomer);
46+
}
47+
48+
@Test
49+
void givenExistingCustomer_whenFoundByName_thenReturnCustomer() throws Exception {
50+
Optional<Customer> customer = customerRepository.findByName("John");
51+
52+
Assertions.assertTrue(customer.isPresent(), "Customer should be present");
53+
Assertions.assertEquals("John", customer.get()
54+
.getName(), "Customer name should match");
55+
}
56+
57+
@Test
58+
void givenNonExistingCustomer_whenFoundByName_thenReturnNull() throws Exception {
59+
Optional<Customer> customer = customerRepository.findByName("Doe");
60+
61+
Assertions.assertFalse(customer.isPresent(), "Customer should not be found");
62+
}
63+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.dbteardown;
2+
3+
import java.util.Optional;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.test.annotation.DirtiesContext;
12+
import org.springframework.test.context.ActiveProfiles;
13+
import org.springframework.test.context.TestPropertySource;
14+
15+
@SpringBootTest
16+
@ActiveProfiles("hsql")
17+
@TestPropertySource(properties = { "spring.jpa.hibernate.ddl-auto=create-drop" })
18+
public class PropertiesIntegrationTest {
19+
20+
@Autowired
21+
private CustomerRepository customerRepository;
22+
23+
@Autowired
24+
private JdbcTemplate jdbcTemplate;
25+
26+
@BeforeEach
27+
void setUp() {
28+
String insertSql = "INSERT INTO customers (name, email) VALUES (?, ?)";
29+
Customer customer = new Customer("John", "[email protected]");
30+
jdbcTemplate.update(insertSql, customer.getName(), customer.getEmail());
31+
}
32+
33+
@Test
34+
@DirtiesContext
35+
void givenCustomer_whenSaved_thenReturnSameCustomer() throws Exception {
36+
Customer customer = new Customer("Doe", "[email protected]");
37+
38+
Customer savedCustomer = customerRepository.save(customer);
39+
40+
Assertions.assertEquals(customer, savedCustomer);
41+
}
42+
43+
@Test
44+
@DirtiesContext
45+
void givenExistingCustomer_whenFoundByName_thenReturnCustomer() throws Exception {
46+
Optional<Customer> customer = customerRepository.findByName("John");
47+
48+
Assertions.assertTrue(customer.isPresent(), "Customer should be present");
49+
Assertions.assertEquals("John", customer.get()
50+
.getName(), "Customer name should match");
51+
}
52+
53+
@Test
54+
@DirtiesContext
55+
void givenNonExistingCustomer_whenFoundByName_thenReturnNull() throws Exception {
56+
Optional<Customer> customer = customerRepository.findByName("Doe");
57+
58+
Assertions.assertFalse(customer.isPresent(), "Customer should not be found");
59+
}
60+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.dbteardown;
2+
3+
import java.util.Optional;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.test.annotation.DirtiesContext;
12+
import org.springframework.test.context.ActiveProfiles;
13+
import org.springframework.test.context.jdbc.Sql;
14+
15+
@SpringBootTest
16+
@ActiveProfiles("hsql")
17+
@DirtiesContext
18+
@Sql(scripts = "/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
19+
public class SqlScriptIntegrationTest {
20+
21+
@Autowired
22+
private CustomerRepository customerRepository;
23+
24+
@Autowired
25+
private JdbcTemplate jdbcTemplate;
26+
27+
@BeforeEach
28+
void setUp() {
29+
String insertSql = "INSERT INTO customers (name, email) VALUES (?, ?)";
30+
Customer customer = new Customer("John", "[email protected]");
31+
jdbcTemplate.update(insertSql, customer.getName(), customer.getEmail());
32+
}
33+
34+
@Test
35+
void givenCustomer_whenSaved_thenReturnSameCustomer() throws Exception {
36+
Customer customer = new Customer("Doe", "[email protected]");
37+
38+
Customer savedCustomer = customerRepository.save(customer);
39+
40+
Assertions.assertEquals(customer, savedCustomer);
41+
}
42+
43+
@Test
44+
void givenExistingCustomer_whenFoundByName_thenReturnCustomer() throws Exception {
45+
Optional<Customer> customer = customerRepository.findByName("John");
46+
47+
Assertions.assertTrue(customer.isPresent(), "Customer should be present");
48+
Assertions.assertEquals("John", customer.get()
49+
.getName(), "Customer name should match");
50+
}
51+
52+
@Test
53+
void givenNonExistingCustomer_whenFoundByName_thenReturnNull() throws Exception {
54+
Optional<Customer> customer = customerRepository.findByName("Doe");
55+
56+
Assertions.assertFalse(customer.isPresent(), "Customer should not be found");
57+
}
58+
}

0 commit comments

Comments
 (0)