mardi 11 août 2020

@SpringBootTest how to avoid a long list of classes

Test classes annotated with @SpringBootTest needs the list of all the dependent classes -

@Component
public class Dao {
    @Autowired
    private Template template;
}

@Component
public class Service {
    @Autowired
    private Dao dao;
}

@Component
public class Controller {
    @Autowired
    private Service service;
}

@SpringBootTest(classes = {Controller.class, Service.class, Dao.class, Template.class})
public void ControllerTest {
}

This can be very tedious to copy paste the names of all the classes for transitive dependencies. Is there any mechanism so that Spring automatically discovers transitive dependencies and resolve them? I just want to pass spring the class Controller and it should be able to automatically discover the other dependencies such as Service, Dao and Template? So far I was not able to succeed in finding anything as a solution by searching google / stackoverflow. The only thing close to this I found are lazy initializing beans and custom slice that are not very neat solutions for this problem. The only solution I can think of is to write some script that spits out a list of dependencies by using reflection.

Aucun commentaire:

Enregistrer un commentaire