2014-05-07 44 views
1

我正在嘗試編寫自定義彈簧數據存儲庫的測試。我也使用QueryDSL。 我是春季數據的新手。我在測試中使用HSQL DB的彈簧支持。 MySQL的開發。春季數據測試自定義存儲庫數據不會更新

問題:如果我使用自定義存儲庫,則在測試中看不到更新的數據。

public interface AuctionRepository extends AuctionRepositoryCustom, CrudRepository<Auction, Long>, QueryDslPredicateExecutor<Auction> { 
// needed for spring data crud 
} 

public interface AuctionRepositoryCustom { 
    long renameToBestName(); 
} 

​​

我的測試
不知何故未能在最後一行

public class CustomAuctionRepositoryImplTest extends AbstractIntegrationTest { 
@Inject 
AuctionRepository auctionRepository; 

@Test 
public void testDoSomething() { 
    Auction auction = auctionRepository.findOne(26L); 
    assertEquals("EmptyName", auction.getName()); 

    // test save 
    auction.setName("TestingSave"); 
    auctionRepository.save(auction); 
    Auction saveResult = auctionRepository.findOne(26L); 
    assertEquals("TestingSave", saveResult.getName()); 

    // test custom repository 
    long updatedRows = auctionRepository.renameToBestName(); 
    assertTrue(updatedRows > 0); 
    Auction resultAuction = auctionRepository.findOne(26L); 
    assertEquals("BestName", resultAuction.getName()); // FAILS expected:<[BestNam]e> but was:<[TestingSav]e> 
} 
} 

我想不通爲什麼使用自定義存儲庫時數據不更新。如果我以開發模式啓動應用程序,並通過控制器調用renameToBestName(),則所有事情都按預期工作,名稱更改。

下面是測試配置如果需要

@RunWith(SpringJUnit4ClassRunner.class) 
@Transactional 
@ActiveProfiles("test") 
@ContextConfiguration(classes = {TestBeans.class, JpaConfig.class, EmbeddedDataSourceConfig.class}) 
@ComponentScan(basePackageClasses = IntegrationTest.class, excludeFilters = @Filter({Configuration.class})) 
public abstract class AbstractIntegrationTest { 
} 

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(basePackageClasses = Application.class) 
class JpaConfig { 

    @Value("${hibernate.dialect}") 
    private String dialect; 
    @Value("${hibernate.hbm2ddl.auto}") 
    private String hbm2ddlAuto; 
    @Value("${hibernate.isShowSQLOn}") 
    private String isShowSQLOn; 

    @Autowired 
    private DataSource dataSource; 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactory.setDataSource(dataSource); 
     entityManagerFactory.setPackagesToScan("auction"); 
     entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 

     Properties jpaProperties = new Properties(); 
     jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect); 
     if (!hbm2ddlAuto.isEmpty()) { 
      jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto); 
     } 
     jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, isShowSQLOn); 
     jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR, "org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor"); 
     entityManagerFactory.setJpaProperties(jpaProperties); 

     return entityManagerFactory; 
    } 

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

'AbstractIntegrationTest'看起來像什麼? – geoand

+0

編輯並添加了一些配置。如果需要,我會添加更多。感謝您的幫助。 – Etruskas

+0

重命名工作如果您嘗試普通的舊JPQL或entityManager.update? – geoand

回答

2

這是由於你的代碼發出的更新查詢被定義爲不驅逐從EntityManager查詢潛在觸摸的物體。閱讀更多關於this answer

相關問題