簡體   English   中英

單元測試期間不執行data.sql

[英]data.sql is not executed during unit test

我正在為基本的 CRUD API 實施單元測試。我有一個名為 Classified 的 class,其中包含一個名為 Category 的 class 實例。 他們都有自己的數據庫。 為了初始化類別數據庫,我在資源文件夾下創建了一個“data.sql”文件。 它在正常執行期間表現出色,但在我運行測試時卻沒有執行。 我正在使用 h2 內存數據庫。

data.sql內容:

insert into CATEGORY values (1, 'Real Estate');
insert into CATEGORY values (2, 'Vehicle');
insert into CATEGORY values (3, 'Shopping');
insert into CATEGORY values (4, 'Other');

測試 class:

@RunWith(SpringJUnit4ClassRunner.class)
public class ClassifiedServiceTest {
@Mock
private ClassifiedRepository classifiedRepository;
@Mock
private ClassifiedHistoryRepository classifiedHistoryRepository;
@Mock
private CategoryRepository categoryRepository;
@InjectMocks
private ClassifiedService classifiedService;

@Test
public void createClassifiedTest() throws FileNotFoundException {
    // This method throws an exception related to CATEGORY table being empty
    }
}

我嘗試將 @Sql("data.sql") 添加到 Test class 但后來我得到了

org.springframework.test.context.transaction.TestContextTransactionUtils - Caught exception while retrieving DataSource for test context [DefaultTestContext@1189dd52 testClass = ClassifiedServiceTest, testInstance = com.sahibinden.codecase.services.ClassifiedServiceTest@1133ec6e, testMethod = createAndDeactivateClassifiedTest@ClassifiedServiceTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@36bc55de testClass = ClassifiedServiceTest, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@a4102b8, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@7a52f2a2, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@75f32542, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@229d10bd], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false, 'org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.mocks' -> org.mockito.internal.configuration.InjectingAnnotationEngine$$Lambda$113/0x0000000800da41d8@355e34c7]]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' 
available

如何配置我的測試 class 以運行 data.sql 並在運行這些測試之前填充數據庫

為此,您應該使用@SpringBootTest讓 Spring Boot 使用 H2 自動配置您的嵌入式數據源。 Spring Boot 然后將執行您的schema.sqldata.sql以自動設置您的 H2 數據庫。 如果您使用@SpringBootTest ,則必須使用@Autowired而不是@InjectMocks來注入您的 bean,並且您不需要模擬其他 bean。

@SpringBootTest
public class ClassifiedServiceTest {

@Autowired
private ClassifiedService classifiedService;

@Test
public void createClassifiedTest() throws FileNotFoundException {
    // This method throws an exception related to CATEGORY table being empty
    }
}

如果啟動 Spring 啟動應用程序的時間太長,您也可以使用切片測試。 為此,你可以這樣做:

@DataJpaTest
@Import(ClassifiedServiceImpl.class) // You need the implementation here 
public class ClassifiedServiceTest {

@Autowired
private ClassifiedService classifiedService;

@Test
public void createClassifiedTest() throws FileNotFoundException {
    // This method throws an exception related to CATEGORY table being empty
    }
}

它只會設置使用 JPA 所需的所有內容,並在應用程序上下文中加載您的ClassifiedService

就像 Farid 說的那樣,您不能在單元測試中使用 Spring 數據,而是在集成測試中使用

@SpringBootTest

用於掛載 SpringBoot 上下文

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM