使用Spring Boot 2.1默认情况下禁用bean overriding,这是一件好事.
但是我确实有一些测试,我使用Mockito用模拟实例替换bean.使用默认设置,由于bean重写,具有此类配置的测试将失败.
我发现工作的唯一方法是通过应用程序属性启用bean覆盖:
spring.main.allow-bean-definition-overriding=true
Run Code Online (Sandbox Code Playgroud)
但是,我真的希望确保我的测试配置的最小bean定义设置,这将由spring指出,覆盖禁用.
我压倒的豆子要么是
我正在考虑的应该是在测试配置中覆盖bean并对其进行打击@Primary,因为我们习惯于数据源配置.然而,这没有任何影响,让我感到疑惑:@Primary残疾豆是否超越自相矛盾?
一些例子:
package com.stackoverflow.foo;
@Service
public class AService {
}
package com.stackoverflow.foo;
public class BService {
}
package com.stackoverflow.foo;
@Configuration
public BaseConfiguration {
@Bean
@Lazy
public BService bService() {
return new BService();
}
}
package com.stackoverflow.bar;
@Configuration
@Import({BaseConfiguration.class})
public class TestConfiguration {
@Bean
public BService bService() {
return Mockito.mock(BService.class);
}
}
Run Code Online (Sandbox Code Playgroud) 我想为我的 RestAPI 端点编写集成测试,但我正在努力解决 @EnableJpaAuditing。我希望 Spring 审核我的一些实体,因此我创建了以下配置类:
@Configuration
@EnableJpaAuditing
public class PersistenceAuditConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
我将其导入到我的主应用程序配置中:
@ServletComponentScan
@SpringBootApplication
@Import(PersistenceAuditConfiguration.class)
public class TMTWebApplication {
public static void main(String[] args) {
SpringApplication.run(TMTWebApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我有一个针对我想要审核的所有实体的抽象基类:
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public abstract class AuditableEntity extends EpicPojo implements Serializable {
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false, updatable = false)
@CreatedDate
private Date createdAt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
@LastModifiedDate
private Date updatedAt;
//...and so on
}
Run Code Online (Sandbox Code Playgroud)
在我的 …