Given a class that does some component testing. I want to mock the fooService.send only one times in the test and use it in aService and bService:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AComponentTestConfiguration.class)
public class AComponentTest {
@Autowired
private AService aService;
@Autowired
private BService bService;
@Autowired
private FooService fooService;
// ... when(fooService.send(any())).thenReturn(response);
}
And the configuration:
@Configuration
public class AComponentTestConfiguration {
private CService cService = new CService(); // 1
@Bean public FooService fooService() { return mock(FooService.class); } // 2
@Bean public BarService barService() { return new BarService(); } // 3
@Bean public AService aService() { return new AService(fooService(), barService(), cService); } // 4
@Bean public BService bService() { return new AService(fooService(), barService(), cService); }
}
How can I use service instances or mocks that must be provided to several beans?
1: Use a private variable in the instances of AService and BService
2, 3: Use functions - that would lead to 2 instances for FooService and BarService, right?
4: Which option must be used for mocks and real service instances?