我已经使用测试容器为 spring boot 项目设置了集成测试(使用 postgresql 设置了一个 docker 实例)。如果我正在测试的存储库不使用本机查询,则测试效果很好。但是,每当存储库包含本机查询时,我都会收到以下错误:ERROR: relation "my_table_here" does not exist
. 如何让我的测试配置工作以允许本机查询?
下面是我的测试设置:
@RunWith(SpringRunner.class)
public class TestPostgresql {
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = PostgresDbContainer.getInstance();
/**
* ************ REPOSITORIES ************
*/
@Autowired
NativeQueryRepository nativeQueryRepository;
@TestConfiguration
@EnableJpaAuditing
@EnableJpaRepositories(
basePackageClasses = {
NativeQueryRepository.class
})
@ComponentScan(
basePackages = {
"com.company.project.package.repository"
}
)
static class PostgresConfiguration {
/**
* ************ DATABASE SETUP ************
*/
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(postgreSQLContainer.getJdbcUrl());
dataSource.setUsername(postgreSQLContainer.getUsername());
dataSource.setPassword(postgreSQLContainer.getPassword());
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new JpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.company.project");
factory.setDataSource(dataSource());
return factory;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory);
return txManager;
}
}
}
编辑:我相信这与命名策略有关?
有关更多上下文,这里是如何在存储库中使用 nativeQuery 的示例
@Repository
public interface NativeQueryRepository extends JpaRepository<NativeEvent, Long> {
@Modifying
@Transactional
@Query(value = "UPDATE native_event SET state = :state " +
"WHERE secondary_id = :secondaryId", nativeQuery = true)
void updateState(
@Param("state") String state,
@Param("secondaryId") String secondaryId);
}
TestPostgresql
我还尝试通过添加注释来更新内部静态类的 testProperties :
@TestPropertySource(properties = {
"spring.jpa.hibernate.naming-strategy=org.springframework.boot.orm.jpa.SpringNamingStrategy"
})
但是,收到的错误没有改变。
编辑:添加NativeEvent
:
@Entity
@Table(
name = "NativeEvent",
indexes = {
@Index(name = "idx_native_event_secondary_id", columnList = "secondaryId")
}
)
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NativeEvent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="secondaryId", nullable=false)
private String secondaryId;
@Column(name="state")
private String state;
}