[英]How to share @MockBeans and mock methods among my tests without inheritance?
I have a base test scenario that will be used by other integration tests.我有一个将被其他集成测试使用的基本测试场景。 This scenario includes some mock beans (
@MockBean
) for external integrations.此场景包括一些用于外部集成的模拟 bean (
@MockBean
)。
Today, I have something like this in the integration test class:今天,我在集成测试课上有这样的事情:
@SpringBootTest
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderIT {
And the fields and annotations to prepare my integration test:以及准备我的集成测试的字段和注释:
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Autowired
private ObjectMapper mapper;
@MockBean
private SomeGateway someGateway;
@MockBean
private SomeRabbitMqService someRabbitMqService ;
@MockBean
private AnotherRabbitMqService anotherRabbitMqService;
@MockBean
private SomeIntegrationService someIntegrationService ;
@MockBean
private Clock clock;
@Before
public void setup() {
//some methods mocking each service above, preparing mockMvc, etc
}
This scenario is necessary for use the MockMvc
and create the main feature in the system, my Order
.此场景对于使用
MockMvc
并在系统中创建主要功能 my Order
是必需的。 This Order
is created by calling a POST method in a Rest API, saving the order
in a memory database.此
Order
是通过调用 Rest API 中的 POST 方法创建的,将order
保存在内存数据库中。
Even this working well, I need to duplicate this block of code containing these @MockBean
and some @Autowired
in another tests, because the Order
is the base scenario to add Products to the order, set an Address to deliver, etc. Each scenario has a different integration test but all of them needs an Order
.即使这样运行良好,我也需要在另一个测试中复制包含这些
@MockBean
和一些@Autowired
的代码块,因为Order
是将产品添加到订单、设置要交付的地址等的基本场景。每个场景都有一个不同的集成测试,但它们都需要一个Order
。
So, how to share the "MockBeans" and the methods that mocks them among my Integration Tests?那么,如何在我的集成测试中共享“MockBeans”和模拟它们的方法? I had really bad experiences using inheritance among the tests and I really would like to try a different approach.
我在测试中使用继承的经历非常糟糕,我真的很想尝试一种不同的方法。
I end up using the Spring profiles .我最终使用了Spring 配置文件。
I created a configuration class annotated with @Profile("test")
and created the mocked beans there.我创建了一个用
@Profile("test")
注释的配置类,并在那里创建了模拟 bean。 Like:像:
@Profile("test")
@Configuration
public class MyMockConfiguration {
@Bean
public SomeService someService() {
SomeService someService = mock(SomeService .class);
// mocked methods and results
return someService ;
}
And in the Test class:在测试类中:
@ActiveProfiles("test")
@SpringBootTest
@WebAppConfiguration
@RunWith(SpringRunner.class)
public class MyControllerIT {
If some integration test needs to override the current mock implementation on the profile, the test just needs to declare @MockBean
on the class and proceed on the mock as usual.如果某些集成测试需要覆盖配置文件上的当前模拟实现,则测试只需要在类上声明
@MockBean
并像往常一样在模拟上进行。
I'm not decide yet if test
is a good name, because for me makes more sense mock the configuration by "context".我还不确定
test
是否是一个好名字,因为对我来说,通过“上下文”来模拟配置更有意义。 So, instead of use the generic name test
on the profile name, I could use createOrder
and have different configuration profiles, each one with a different name and different mocks: createOrder
, createOrderWithoutProducts
.因此,我可以使用
createOrder
并使用不同的配置文件,而不是在配置文件名称上使用通用名称test
,每个配置文件都有不同的名称和不同的模拟: createOrder
、 createOrderWithoutProducts
。
I believe @ContextConfiguration
was created for this purpose: https://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles我相信
@ContextConfiguration
是为此目的创建的: https ://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.