Let say we have
@Component
public class MyService {
private ServiceA serviceA;
private ServiceB serviceB;
@Autowired
public MyService(ServiceA serviceA, ServiceB serviceB) {
this.serviceA = serviceA;
this.serviceB = serviceB;
}
}
@Component
public class ServiceA {
private ServiceC serviceC;
@Autowired
public ServiceA(ServiceC serviceC) {
this.serviceC = serviceC;
}
}
@Component
public class ServiceB {
@Autowired
private ServiceE serviceE;
}
Then I have my Groovy/Spock test:
@ContextConfiguration(
loader = SpringApplicationContextLoader,
classes = ManagementApplication.class
)
@WebAppConfiguration
class MyTest extends Specification {
@InjectMock
private MyService myService
@Mock
private ServiceA serviceA
def setup() {
MockitoAnnotations.initMocks(this)
}
}
So as you can see MyService injects ServiceA and ServiceB with constructor injection. ServiceA also uses constructor injection while ServiceB uses field injection. In my test I want to mock ServiceA but to Autowire ServiceB. However ServiceA seems to be injected normally (not mocked). Which is the appropriate way to do this?
Aucun commentaire:
Enregistrer un commentaire