亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot DBUnit 單元測試(小結(jié))

 更新時間:2018年09月11日 10:26:29   作者:_Walli_  
這篇文章主要介紹了SpringBoot DBUnit 單元測試(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

DBunit 是一種擴展于JUnit的數(shù)據(jù)庫驅(qū)動測試框架,它使數(shù)據(jù)庫在測試過程之間處于一種已知狀態(tài),如果一個測試用例對數(shù)據(jù)庫造成了破壞性影響,它可以幫助避免造成后面的測試失敗或者給出錯誤結(jié)果。

DBunit通過維護(hù)真實數(shù)據(jù)庫與數(shù)據(jù)集(IDataSet)之間的關(guān)系來發(fā)現(xiàn)與暴露測試過程中的問題。IDataSet 代表一個或多個表的數(shù)據(jù)。此處IDataSet可以自建,可以由數(shù)據(jù)庫導(dǎo)出,并以多種方式體現(xiàn),xml文件、XLS文件和數(shù)據(jù)庫查詢數(shù)據(jù)等。

基于DBUnit 的測試的主要接口是IDataSet,可以將數(shù)據(jù)庫模式的全部內(nèi)容表示為單個IDataSet 實例。這些表本身由Itable 實例來表示。

1. pom文件配置

    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
    </dependency>
    <dependency>
      <groupId>com.github.springtestdbunit</groupId>
      <artifactId>spring-test-dbunit</artifactId>
      <version>1.3.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.dbunit</groupId>
      <artifactId>dbunit</artifactId>
      <version>2.5.3</version>
    </dependency>

2. 由于spring-test-dbunit 的注解配置,導(dǎo)致 mockito 中的 @MockBean 失效,需要將 MockitoTestExecutionListener 和 DbUnitTestExecutionListener 配置到 @TestExecutionListeners 中

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@TestExecutionListeners({
  DependencyInjectionTestExecutionListener.class,
  DirtiesContextTestExecutionListener.class,
  TransactionalTestExecutionListener.class,
  MockitoTestExecutionListener.class,
  DbUnitTestExecutionListener.class
})
@DatabaseSetup("/test-data.xml")
public class OrderInfoControllerTest {
}

3. DBUnit 的數(shù)據(jù)文件 xml 中允在空值的配置

package com.test; 
import com.github.springtestdbunit.bean.DatabaseConfigBean;
import com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 *
 * @author walli
 */
@Configuration
public class DBUnitConfiguration {
  
  private static final Logger LOGGER = LoggerFactory.getLogger(DBUnitConfiguration.class);
  
  @Bean
  public DatabaseConfigBean dbUnitDatabaseConfig() {
    DatabaseConfigBean bean = new DatabaseConfigBean();
    bean.setAllowEmptyFields(true);
    return bean;
  }
 
  @Bean
  public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection(
      DatabaseConfigBean dbUnitDatabaseConfig,
      DataSource dataSource) {
    DatabaseDataSourceConnectionFactoryBean bean = new DatabaseDataSourceConnectionFactoryBean(dataSource);
    bean.setDatabaseConfig(dbUnitDatabaseConfig);
    
    return bean;
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中的transient關(guān)鍵字解析

    Java中的transient關(guān)鍵字解析

    這篇文章主要介紹了Java中的 transient關(guān)鍵字解析,有時候我們的一些敏感信息比如密碼并不想序列化傳輸給對方,這個時候transient關(guān)鍵字就派上用場了,如果一個類的變量加上了transient關(guān)鍵字那么這個字段就不會被序列化,需要的朋友可以參考下
    2023-09-09
  • Java HttpURLConnection使用方法詳解

    Java HttpURLConnection使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java HttpURLConnection使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • SpringBoot Jpa企業(yè)開發(fā)示例詳細(xì)講解

    SpringBoot Jpa企業(yè)開發(fā)示例詳細(xì)講解

    這篇文章主要介紹了SpringBoot Jpa企業(yè)開發(fā)示例,Jpa可以通過實體類生成數(shù)據(jù)庫的表,同時自帶很多增刪改查方法,大部分sql語句不需要我們自己寫,配置完成后直接調(diào)用方法即可,很方便
    2022-11-11
  • PostMan post請求發(fā)送Json數(shù)據(jù)的方法

    PostMan post請求發(fā)送Json數(shù)據(jù)的方法

    下面小編就為大家分享一篇PostMan post請求發(fā)送Json數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實現(xiàn)方法

    java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實現(xiàn)方法

    這篇文章主要介紹了java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • java并發(fā)數(shù)據(jù)包Exchanger線程間的數(shù)據(jù)交換器

    java并發(fā)數(shù)據(jù)包Exchanger線程間的數(shù)據(jù)交換器

    這篇文章主要為大家介紹了java并發(fā)數(shù)據(jù)包使用數(shù)據(jù)交換器Exchanger來進(jìn)行線程之間的數(shù)據(jù)交換。有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-03-03
  • 基于java中泛型的總結(jié)分析

    基于java中泛型的總結(jié)分析

    本篇文章介紹了,在java中泛型的總結(jié)分析。需要的朋友參考下
    2013-05-05
  • spring中的ObjectPostProcessor詳解

    spring中的ObjectPostProcessor詳解

    這篇文章主要介紹了spring中的ObjectPostProcessor詳解,Spring Security 的 Java 配置不會公開其配置的每個對象的每個屬性,這簡化了大多數(shù)用戶的配置,畢竟,如果每個屬性都公開,用戶可以使用標(biāo)準(zhǔn) bean 配置,需要的朋友可以參考下
    2024-01-01
  • SpringBoot中實現(xiàn)Redis?Stream隊列的代碼實例

    SpringBoot中實現(xiàn)Redis?Stream隊列的代碼實例

    本文介紹了如何在Spring?Boot中使用Redis?Stream隊列進(jìn)行消息的生產(chǎn)和消費,涉及到的主要內(nèi)容包括添加Redis依賴、配置RedisTemplate、創(chuàng)建生產(chǎn)者和消費者監(jiān)聽器等,需要的朋友可以參考下
    2024-09-09
  • 詳解spring cloud hystrix緩存功能的使用

    詳解spring cloud hystrix緩存功能的使用

    這篇文章主要介紹了詳解spring cloudhystrix緩存功能的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論