Home > database >  How to tell SpringBootTest to load all required classes without explicitly having to specify them
How to tell SpringBootTest to load all required classes without explicitly having to specify them

Time:01-10

As far as I understand, you have two options with SpringBootTests:

  1. Load the whole application
  2. Load only what you need by specifying the classes explicitly

However, if you do 2., depending on how large the part of the application is you want to test, you'll end up with a long list of classes


@pringBootTest
@ContextConfiguration(classes = {
        A.class, B.class, C.class, D.class, E.class,
        F.class, G.class, H.class, I.class, J.class, 
        K.class, L.class, M.class, N.class, O.class, 
        P.class, Q.class, R.class
})

And whenever parts of what you want to test change, you have to figure out what beans are missing and manually add them to the list.

Is there any way to tell Spring if you want to test A.class to automatically detect and load the dependents automatically?

B.class, C.class, D.class, E.class, F.class, G.class, H.class, I.class, J.class, K.class, L.class, M.class, N.class, O.class, P.class

CodePudding user response:

Just omit the @ContextConfiguration completely. @SpringBootTest will then create the whole application context.

CodePudding user response:

There is a way for reducing manual work. First, need to group all the beans need to test your class in a configuration class annotated with @Configuration .

    @Configuration
    public class ConfigClass{

//define all necessary Beans here required for testing

    }

Then need to give the class annotated with @Configuration as a value to classes attribute

@ContextConfiguration(classes={ConfigClass.class})
  • Related