【发布时间】:2019-06-17 07:40:30
【问题描述】:
我有许多不同的 SpringBoot 测试正在运行。到目前为止,auto configuration slices 确实很有帮助,尤其是与@MockBean 结合使用。
但在我当前的测试中,没有这样的切片适合并且使用 @SpringBootTest 启动完整的上下文太慢了。
有没有办法手动设置要启动的对象树的顶端,并从那里弹簧自动装配所有需要的 bean?或者有没有办法手动设置所有需要的bean?
在我的具体情况下,我想测试一个 MapStruct 生成的映射器(使用componentModel = "spring"),这个映射器使用另外两个映射器,每个映射器都注入一个服务来完成他们的工作。
服务通过@MockBean提供:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductResponsibleUnitMapperTest {
@Autowired
private PRUMapper mapper;
@MockBean
private TradingPartnerService tradingPartnerService;
@MockBean
private ProductHierarchyService productHierarchyService;
@Test
public void mapForthAndBack(){
//works but takes ages to boot
}
}
我无法在映射器(用于服务)上使用构造函数注入,因为 MapStruct 不会生成正确的实现。
如何获取只包含所需 bean 的 Spring-Context?
【问题讨论】:
-
写给构造函数注入:你试过了吗:InjectionStrategy.CONSTRUCTOR (MapStruct 1.3Beta2)?
-
很高兴知道这会来
标签: spring spring-boot mockito spring-boot-test mapstruct