2017-04-13 149 views
0

我正在嘗試使用HikariCP和H2以及rxjava-jdbc進行測試。我的代碼在連接到真實數據庫時工作正常,但是當切換到H2時,即使我沒有在內存中執行,我似乎也無法讓表格粘住。使用H2和HikariCP - 未找到表

代碼:

@RunWith(SpringRunner.class) 
@ContextConfiguration(loader=AnnotationConfigContextLoader.class) 
public class BidsRepositoryTest { 

    private static final Logger log = LoggerFactory.getLogger(BidsRepositoryTest.class); 

    @Configuration 
    static class ContextConfiguration { 

     // this bean will be injected into the OrderServiceTest class 
     @Bean 
     public BidsRepository bidsRepository() { 
      BidsRepository bidsRepository = new BidsRepositoryImpl(); 
      // set properties, etc. 
      return bidsRepository; 
     } 

     @Bean(destroyMethod = "close") 
     public DataSource dataSource() throws SQLException { 
      HikariConfig config = new HikariConfig(); 
      config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); 
      config.setConnectionTestQuery("VALUES 1"); 
      config.addDataSourceProperty("URL", "jdbc:h2:~/testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MSSQLServer"); 
      config.addDataSourceProperty("user", "sa"); 
      config.addDataSourceProperty("password", "sa"); 
      HikariDataSource ds = new HikariDataSource(config); 

      HikariDataSource dataSource = new HikariDataSource(config); 

      return dataSource; 
     } 

     @Bean 
     public ConnectionProvider connectionProvider(){ 
      return new CustomConnectionProvider(); 
     } 
    } 

    @Autowired 
    private BidsRepository bidsRepository; 

    @Autowired 
    ConnectionProvider connectionProvider; 


    @Test 
    public void shouldSelectEntityForMatchingIds(){ 

     int userId = 1012; 
     int listingId = 2; 

     Database db = Database.builder().connectionProvider(connectionProvider).build(); 
     db.update("create table Bids\n" + 
       "(\n" + 
       "\tID int not null,\n" + 
       "\tUserID int not null\n" + 
       "\tAmount money not null,\n" + 
       "\tParticipation money default 0 not null,\n" + 
       "\tMinRate decimal(6,3) not null,\n" + 
       "\tListingID int not null\n" + 
       "\tStandingBidID int default (-1) not null,\n" + 
       "\tFundingAccountID int not null\n" + 
       "\tCreationDate datetime default getdate() not null,\n" + 
       "\tModifiedDate datetime default getdate() not null,\n" + 
       "\tCollectionAgencyID int\n" + 
       "\tWithdrawn bit default 0 not null,\n" + 
       "\tAnonymous bit default 0 not null,\n" + 
       "\tAltKey varchar(30) default substring(convert(varchar50,newid()),1,4) + convert(varchar50,(convert(bigint,getdate()) * 86400000 + datepart(hour,getdate()) * 3600000 + datepart(minute,getdate()) * 60000 + datepart(second,getdate()) * 1000 + datepart(millisecond,getdate()) + abs(checksum(newid())))) + substring(convert(varchar50,newid()),30,6) not null,\n" + 
       "\tAltKeySearchID as checksum([AltKey]),\n" + 
       "\tBidSourceID tinyint not null\n" + 
       "\tMinYield decimal(6,3) not null,\n" + 
       "\tInvestmentOrderID int\n" + 
       "\tconstraint PK_Bids\n" + 
       "\t\tprimary key (ID, ListingID)\n" + 
       ") go"); 


     db.update("INSERT INTO Bids (ID, UserId, Amount, Participation, MinRate, ListingId, BidSourceId, MinYield) " 
       + " VALUES(1, 1012, 10000, 3000, 0.06, 2, 2, 12000) go"); 


     bidsRepository.getBid(userId, listingId).forEach(a-> { 
        assertThat(a.getUserId()).isEqualTo(userId); 
        assertThat(a.getListingId()).isEqualTo(listingId); 
       } 
     ); 
    } 
} 

異常: rx.exceptions.OnErrorNotImplementedException:表 「出價」 未找到; SQL語句:(省略);

編輯: 通過這個線程去了,但未能找到一個解決方案: H2 in-memory database. Table not found

回答

0

這段代碼的問題不是H2的設置。 H2正在執行,因爲它是我對rxjava-jdbc代碼的誤解 - 創建/插入語句在最後需要一個.execute()來運行它們。

Database db = Database.builder().connectionProvider(connectionProvider).build(); 
     db.update("create table Bids\n" + 
       "(\n" + 
       "\tID int not null,\n" + 
       "\tUserID int not null,\n" + 
       "\tAmount decimal not null,\n" + 
       "\tParticipation decimal default 0 not null,\n" + 
       "\tMinRate decimal(6,3) not null,\n" + 
       "\tListingID int not null,\n" + 
       "\tStandingBidID int default (-1) not null,\n" + 
       "\tFundingAccountID int not null,\n" + 
       "\tCreationDate datetime default getdate() not null,\n" + 
       "\tModifiedDate datetime default getdate() not null,\n" + 
       "\tCollectionAgencyID int,\n" + 
       "\tWithdrawn bit default 0 not null,\n" + 
       "\tAnonymous bit default 0 not null,\n" + 
       "\tAltKey varchar(30) default 'ABC123' not null,\n" + 
       "\tAltKeySearchID VARCHAR(30),\n" + 
       "\tBidSourceID tinyint not null,\n" + 
       "\tMinYield decimal(6,3) not null,\n" + 
       "\tInvestmentOrderID int\n" + 
       ")").execute();