How to do integration testing with spring and junit by scanning classpath instead of manually specifying classes to autowire?


I’m trying to do some sort of integration testing (not really full testing, only it’s not really unit testing either) on a service :

package foo;

public class FooService {}

And the corresponding test :

package foo;

public FooTest {
   @Autowired FooService fooService;

Only when I try to test it says

No qualifying bean of type 'foo.FooService' available

I could add the class to @SpringBootTest like this :

@SpringBootTest(classes = {FooService.class})

But I don’t want to manually select the classes I want to test. My application is much larger than the MWE, and I want to test without having to specify each class I am testing and their respective dependencies.

Is there anyway to tell spring to scan and autowire dependencies like it would outside of tests ?


add @Service annotaion to the FooService class

Answered By – Mani

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published