我已经实现了自己的类来表示基于 UserType 类的数据库矢量数据。
我的例子:
class MyVectorType implements UserType {
@Override
public int[] sqlTypes() {
return new int[] { Types.ARRAY };
}
};
@Entity
@Table("MY_ENTITY")
public class MyEntity {
private MyVectorType myVectorType;
}
但是,此类不能用于使用 h2 方言进行测试,即。在内存数据库中。有错误:没有 JDBC 类型的方言映射:2003。
因此,我想从测试中排除此实体(包括存储库),但这不起作用:
@SpringBootApplication
@ComponentScan(excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
MyEntity.class, MyEntityRepository.class})
})
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
有什么问题或者有什么解决这个问题的最佳实践?
编辑 1:固定示例 - 添加了正确的实体和存储库
解决方案 1:
我认为此时唯一可能的解决方案是将实体类(需要排除)移动到其他包。然后设置@EntityScan 只扫描未排除的包。 ComponentScan 中的排除过滤器似乎只适用于 @Component 类,而不适用于 @Entity。然而,这并不是解决这个问题的绝对最佳实践。
最佳答案
只需将其定义为 @MockBean,这样您的存储库的实际实现将被测试中的无功能模拟替换:
@MockBean
private MyVectorRepositoryType vectorRepository;
关于Spring Boot如何从测试中排除某些类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50250024/