简体   繁体   English

如何在没有继承的情况下在我的测试中共享@MockBeans 和模拟方法?

[英]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 ,每个配置文件都有不同的名称和不同的模拟: createOrdercreateOrderWithoutProducts

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.

相关问题 如何在测试过程中模拟spring组件而不从中提取接口? - How mock spring component during tests without extracting interface from it? 我的 rest 服务依赖于外部服务。 如何模拟它以在我的 cucumber 测试中使用? - My rest services relies on an external service. How do I mock it to use on my cucumber tests? Spring Boot @MockBeans - 如何在多个测试类中使用相同的 bean - Spring Boot @MockBeans - How to use the same beans in multiple test classes 如何在几个ResourceBundle之间共享与语言环境无关的属性? - How to share locale independent properties among several ResourceBundles? 使用redis的Spring会话 - 如何在集成测试中模拟它? - Spring session with redis - how to mock it in integration tests? 如何在Spring单元测试中快速模拟服务? - How to quickly mock services in spring unit tests? 如何使用 @Transactional 注释测试方法 - How tests methods with @Transactional anotation 如何为集成测试创建模拟 VaadinSession? - How to create mock VaadinSession for integration tests? HikariCP - 如何为单元测试配置模拟数据源 - HikariCP - how to configure a mock datasource for unit tests 使用 InjectMocks,我的一个模拟变成 null,没有它,另一个模拟是 null - With InjectMocks, one of my mock become null, without it, an other Mock is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM