【发布时间】:2014-06-24 07:08:58
【问题描述】:
我正在尝试为自定义 spring 数据存储库编写测试。我也在使用 QueryDSL。 我是弹簧数据的新手。我在测试中使用了对 HSQL DB 的 spring 支持。用于开发的 MySQL。
问题:如果我使用自定义存储库,我在测试中看不到更新的数据。
public interface AuctionRepository extends AuctionRepositoryCustom, CrudRepository<Auction, Long>, QueryDslPredicateExecutor<Auction> {
// needed for spring data crud
}
.
public interface AuctionRepositoryCustom {
long renameToBestName();
}
.
public class AuctionRepositoryImpl extends QueryDslRepositorySupport implements AuctionRepositoryCustom {
private static final QAuction auction = QAuction.auction;
public AuctionRepositoryImpl() {
super(Auction.class);
}
@Override
public long renameToBestName() {
return update(auction)
.set(auction.name, "BestName")
.execute();
}
}
我的测试
不知何故在最后一行失败了
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();
}
}
【问题讨论】:
-
AbstractIntegrationTest长什么样子? -
编辑并添加了一些配置。如果需要,我会添加更多。感谢您提供帮助。
-
如果您尝试普通的旧 JPQL 或 entityManager.update,重命名是否有效?
-
我试过
entityManager.refresh(resultAuction);- 测试通过了。这很好:-) 谢谢。但是,为什么需要这条线?也许我缺少一些配置? -
我不太清楚,因为我使用 QueryDSL 的次数不多
标签: java hibernate spring-data querydsl