SpringBoot uses EmbeddedDatabaseBuilder for database integration testing

SpringBoot uses EmbeddedDatabaseBuilder for database integration testing

In SpringBoot applications, we can use EmbeddedDatabaseBuilder for database integration testing. EmbeddedDatabaseBuilder is a test tool that allows us to start an embedded database in memory and test it. Using EmbeddedDatabaseBuilder allows us to easily test the behavior of the database without actually connecting to the database.

In this article, we will introduce how to use EmbeddedDatabaseBuilder for database integration testing, and how to write test cases to test the behavior of the database.

insert image description here

What is EmbeddedDatabaseBuilder

EmbeddedDatabaseBuilder is a test tool in the Spring framework, which can start an embedded database in memory and test it. EmbeddedDatabaseBuilder allows us to easily test the behavior of the database without actually connecting to the database.

EmbeddedDatabaseBuilder can be used to test database-related components such as Spring Data JPA, Hibernate, and MyBatis. EmbeddedDatabaseBuilder can start a variety of embedded databases, including H2, HSQLDB, Derby, etc.

Database integration testing with EmbeddedDatabaseBuilder

In SpringBoot, we can use EmbeddedDatabaseBuilder for database integration testing. EmbeddedDatabaseBuilder can be set up with the following steps:

  1. import dependencies

Add the following dependencies to the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>
  1. Create test class

Create a test class and add @RunWith(SpringRunner.class)and @SpringBootTestannotations to use the SpringBoot context in the test.

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmbeddedDatabaseBuilderTest {
    
    

}
  1. Inject DataSource

In the test class, we need to inject the DataSource object in order to use the embedded database in the test. We can inject DataSource objects using @Autowiredannotations .

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmbeddedDatabaseBuilderTest {
    
    

    @Autowired
    private DataSource dataSource;

}
  1. Create an embedded database

In the test method, we need to create the embedded database using EmbeddedDatabaseBuilder. We can create an embedded database with the following code:

EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
    .setType(EmbeddedDatabaseType.H2)
    .addScript("classpath:schema.sql")
    .addScript("classpath:data.sql")
    .build();

In the above example, we use the EmbeddedDatabaseBuilder()constructor to create the embedded database, and use setType(EmbeddedDatabaseType.H2)the method to set the database type to H2. We also use addScript()the method to add scripts for creating database tables and inserting test data.

  1. Execute the test case

In the test method, we can use the DataSource object to connect to the embedded database and execute SQL statements. We can execute SQL statements using the JdbcTemplate object.

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM users");

In the above example, we use JdbcTemplatethe object to connect to the embedded database and execute the SQL statement. We use queryForList()the method to query the database table and return the result set.

test database

In SpringBoot, we can use EmbeddedDatabaseBuilder for database integration testing to test the behavior of the database. Below we will write a test case to test the database.

First, we need to write a SQL script for creating database tables and inserting test data. We can create schema.sqla and a data.sqlfile for creating database tables and inserting test data.

schema.sqldocument:

CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE
);

data.sqldocument:

INSERT INTO users (id, name, email) VALUES
(1, 'John', '[email protected]'),
(2, 'Jane', '[email protected]'),
(3, 'Bob', '[email protected]');

Next, we'll write a test case to test the behavior of the database. We will write test cases using JUnit framework.

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmbeddedDatabaseBuilderTest {
    
    

    @Autowired
    private DataSource dataSource;

    @Test
    public void testEmbeddedDatabaseBuilder() {
    
    
        EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:schema.sql")
            .addScript("classpath:data.sql")
            .build();

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM users");

        assertEquals(3, result.size());
        assertEquals("John", result.get(0).get("name"));
        assertEquals("[email protected]", result.get(1).get("email"));

        db.shutdown();
    }
}

In the above example, we use EmbeddedDatabaseBuilder to create an embedded database, and connect to the database to execute SQL statements. We execute the SQL statement using the JdbcTemplate object and compare the expected and actual results for equality through assertions.

Summarize

In this article, we introduced how to use EmbeddedDatabaseBuilder for database integration testing. Using EmbeddedDatabaseBuilder allows us to easily test the behavior of the database without actually connecting to the database. We also wrote a test case to test the behavior of the database and used the JUnit framework for testing.

In actual development, we should write more test cases to ensure that the database behaves as expected. Using EmbeddedDatabaseBuilder can help us write more efficient and accurate database test cases, thereby improving code quality and development efficiency.

Guess you like

Origin blog.csdn.net/it_xushixiong/article/details/131371902