Spring Test


  1. Spring Boot 테스트 기능 소개

  2. Spring Boot 테스트 관련 Use-Case

  3. Spring Boot EmbeddedDatabase 설정 시 빈 등록 샘플 코드

  1. @DataJpaTest을 사용한 DataJpa 테스트 예제 코드
  2. JpaVendorAdapter 인터페이스를 구현 객체를 @Bean으로 동록하여 EntityManagerFactoryBean 등록 예제 코드
    • 테스트 내장 디비 설정 시 사용.
    • 링크 : https://stackoverflow.com/questions/21968965/disable-table-recreation-in-spring-boot-application
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      @ComponentScan
      @EnableAutoConfiguration
      @EnableHypermediaSupport
      @EnableSpringDataWebSupport
      public class ApplicationConfig {

      @Bean
      public DataSource dataSource() {
      DriverManagerDataSource datasource = new DriverManagerDataSource();
      datasource.setDriverClassName("org.postgresql.Driver");
      datasource.setUrl("jdbc:postgresql://localhost/mydatabase");
      datasource.setUsername("myusername");
      datasource.setPassword("mypassword");
      return datasource;
      }

      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
      LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
      lef.setDataSource(dataSource);
      lef.setJpaVendorAdapter(jpaVendorAdapter);
      lef.setPackagesToScan("my.domain.package");
      Properties jpaProperties = new Properties();
      jpaProperties.setProperty("hibernate.hbm2ddl.auto", "update");
      lef.setJpaProperties(jpaProperties);
      return lef;
      }

      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
      HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
      hibernateJpaVendorAdapter.setShowSql(false);
      hibernateJpaVendorAdapter.setGenerateDdl(true);
      hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
      return hibernateJpaVendorAdapter;
      }

      @Bean
      public PlatformTransactionManager transactionManager() {
      return new JpaTransactionManager();
      }

      public static void main(String[] args) {
      SpringApplication.run(ApplicationConfig.class, args);
      }
      }