是否可以@MockBean用真实的代替继承的@Bean?我有一个抽象类,它为所有 ITest 定义了许多配置和设置。仅在一次测试中,我想使用真正的 bean,而不是使用模拟的 bean。但仍然继承其余的配置。@Servicepublic class WrapperService { @Autowired private SomeService some;}@RunWith(SpringRunner.class)@SpringBootTest(...)public abstract class AbstractITest { //many more complex configurations @MockBean private SomeService service;}public class WrapperServiceITest extends AbstractITest { //usage of SomeService should not be mocked //when calling WrapperService //using spy did not work, as suggested in the comments @SpyBean private SomeService service;;}
2 回答
![?](http://img1.sycdn.imooc.com/5458502c00012d4a02200220-100-100.jpg)
蛊毒传说
TA贡献1895条经验 获得超3个赞
找到了一种@Configuration在属性上使用测试条件的方法,并在 impl 中覆盖该属性@TestPropertySource:
public abstrac class AbstractITest {
@TestConfiguration //important, do not use @Configuration!
@ConditionalOnProperty(value = "someservice.mock", matchIfMissing = true)
public static class SomeServiceMockConfig {
@MockBean
private SomeService some;
}
}
@TestPropertySource(properties = "someservice.mock=false")
public class WrapperServiceITest extends AbstractITest {
//SomeService will not be mocked
}
添加回答
举报
0/150
提交
取消