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

标签 spring spring-boot h2 dialect usertype

我已经实现了自己的类来表示基于 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/

相关文章:

java - 尝试使用 spring 部署 liferay portlet 时出现异常

spring-boot - 连接关闭异常 : Premature end of chunk coded message body with apache hhtpclient 4. 5.5

java - H2连接池

playframework - Play 框架 Ebean BigDecimal 分数

hibernate - H2 查询失败并显示 'No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode'

java - 使用 Spring MVC 和 Hibernate 在 JSP 中显示 jpg 图像列表

java - 记录 Spring 通过 @Async 注释创建的线程抛出的 RuntimeException

java - 动态加载的 spring 上下文

java - spring boot 未加载环境的正确 Jasypt application.properties

java - 如何使用 Spock 在 Spring Boot 过滤器中模拟服务或 stub 服务方法?