java - Spring自定义注解集成测试

标签 java spring spring-mvc annotations

在我的每个集成测试中,我都必须添加以下注释

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:test-context.xml")
@Transactional

我在想是否可以创建一个自定义注释来对以上注释进行分组,而不是添加 3 个注释我只能添加一个。为此,我创建了以下注释。

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration("classpath:test-context.xml")
public @interface IntegrationTest {
  String[] value() default {};
}

但是当我在我的测试类上添加 @IntegrationTest 时,它不会加载上下文。谁能指导我缺少什么?

谢谢

最佳答案

尝试分别添加@RunWith(SpringJUnit4ClassRunner.class)。

@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
public ControllerTest

这是父类。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration("classpath:test-context.xml")
public @interface IntegrationTest

此外,我会避免使用名称“@IntegrationTest”,因为它是 Spring Boot 中的默认注释。

关于java - Spring自定义注解集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25070524/

相关文章:

java - SpringMVC 验证问题

Spring MVC 与 Hibernate Validator 对于数据库字段是必需的,但在应用程序中不是必需的

java - 在 Amazon S3 中使用 KMS key 解密文件时出错

java - EJB 3 注入(inject) spring bean

java - API 级别 19 的工具栏 setOnScrollChangeListener()

spring - Controller 连接终止后的事务回滚(即客户端超时)

java - Spring MVC + Spring Security - 使用 JSON 的 Controller 身份验证

java - 在Java中,“this”在构造函数中是否可以为null?

java - hibernate 、MySql、 Spring

Spring安全标签库