【问题标题】:How to efficiently test a JPA repository?如何有效地测试 JPA 存储库?
【发布时间】:2021-12-21 12:02:39
【问题描述】:

假设以下类及其存储库:

import javax.persistence.*;
import java.io.Serializable;

@Entity
public class Student implements Serializable {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    
    //getters , setters, Contructors
    
}

存储库是:

import org.springframework.data.jpa.repository.JpaRepository;
    
public interface StudentRepository extends JpaRepository<Student, Long> {}

我所做的测试是:为 H2 数据库配置 application.properties 并创建以下类:

import com.ndongoel.myDHL.repositories.AdresseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoadDB {
    private static final Logger log = LoggerFactory.getLogger(LoadDB.class);

    @Bean
    CommandLineRunner initDatabase(AdresseRepository adresseRepository) {
        return args -> {
            Student stu = new Student(null,"Mike","Smith");
            log.info("Preloading " + studentRepository.save(stu));
    }
}

只需检查控制台和 H2 数据库!

【问题讨论】:

  • 您可以使用测试容器进行集成测试。

标签: java spring spring-boot jpa spring-data-jpa


【解决方案1】:

使用testcontainers 配置测试如下:

mport org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import org.testcontainers.containers.PostgreSQLContainer;

import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(initializers = {InternalErrorTest.Initializer.class})
@AutoConfigureMockMvc
public class InternalErrorTest {
    @Autowired
    private MockMvc mockMvc;

    @BeforeClass
    public static void setTest() {
        DockerProxy.setTesting(true);
        postgreSQLContainer.start();
    }

    @ClassRule
    public static PostgreSQLContainer postgreSQLContainer =
            new PostgreSQLContainer("postgres:11.1")
                    .withDatabaseName("myName")
                    .withUsername("myUsername")
                    .withPassword("myPassword");

    static class Initializer
            implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            Properties props = new Properties();
              props.setProperty("hibernate.connection.url", testUrl);
        }
    }


    //your tests...

}

【讨论】:

    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 2021-07-28
    • 2018-06-26
    • 2018-07-30
    • 2022-10-13
    • 2019-05-12
    • 1970-01-01
    • 2019-08-31
    相关资源
    最近更新 更多