I have a base test scenario that will be used by other integration tests. This scenario includes some mock beans (@MockBean
) for external integrations.
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
. This Order
is created by calling a POST method in a Rest API, saving the order
in a memory database.
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
.
So, how to share the "MockBeans" and the methods that mocks them among my Integration Tests? I had really bad experiences using inheritance among the tests and I really would like to try a different approach.