dimanche 21 juillet 2019

Mock multiple beans in SpringBoot

I have a test in SpringBoot that should check multiple beans in very same way:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EndpointsTest implements ApplicationContextAware {

    @MockBean
    ComponentA componentA;
    @MockBean
    ComponentB componentB;

...

Since I don't want to write a new dummy test for each component, I though to fetch all beans (hence the usage of ApplicationContextAware) and then to run test on each such bean.

Of course, these beans have to be mocked.

Is there a way to manually mock a bean like the MockBean annotation does and replace it the Spring context? I want also to remove explicit declaration of all components (the number is not small), just for the purpose of mocking.

Aucun commentaire:

Enregistrer un commentaire