Spring Boot如何从测试中排除某些类

Luk*_*uke 4 spring h2 usertype dialect spring-boot

我已经实现了自己的类来表示基于 UserType 类的数据库矢量数据。

我的例子:

class MyVectorType implements UserType {
@Override
    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };
    }
};

@Entity
@Table("MY_ENTITY")
public class MyEntity {
    private MyVectorType myVectorType;

}
Run Code Online (Sandbox Code Playgroud)

但是,此类不能用于使用 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么问题或者有什么解决这个问题的最佳实践?

编辑 1:固定示例 - 添加了正确的实体和存储库

解决方案 1: 我认为目前唯一可能的解决方案是将实体类(需要排除)移动到其他包。然后设置@EntityScan 只扫描未排除的包。ComponentScan 中的排除过滤器似乎只适用于 @Component 类,而不适用于 @Entity。然而,这并不是解决这个问题的绝对最佳实践。

Plo*_*log 7

只需将其定义为 @MockBean,这样您的存储库的实际实现将被测试中的无功能模拟替换:

@MockBean
private MyVectorRepositoryType vectorRepository;
Run Code Online (Sandbox Code Playgroud)