gpt4 book ai didi

java - Spring数据测试自定义存储库数据不更新

转载 作者:行者123 更新时间:2023-11-30 03:54:43 31 4
gpt4 key购买 nike

我正在尝试为自定义 Spring 数据存储库编写测试。我也在使用 QueryDSL。我是 spring-data 的新手。我在测试中使用 Spring 对 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 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>
}
}

我不明白为什么使用自定义存储库时数据不更新。如果我在开发模式下启动应用程序,并通过 Controller 调用 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();
}
}

最佳答案

这是因为通过代码发出的更新查询被定义为不会从 EntityManager 中逐出该查询可能涉及的对象。阅读 this answer 了解更多相关信息.

关于java - Spring数据测试自定义存储库数据不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515555/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com