0

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

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136

2 Answers2

0

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

times29
  • 2,782
  • 2
  • 21
  • 40
  • My example is coming from a `@CamelSpringBootTest` where you need the `@ContextConfiguration`. But this is exactly not what I want, because using just `@SpringBootTest` is really slow if the application is a bit bigger, as everything is loaded – Marian Klühspies Jan 09 '23 at 15:16
0

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})
  • So you would do this just for the tests or also use it in the application code itself? If it is only for the test, it seems it just relocates the problem, and if it should be used in the application, it would mean bloating the code, because you'd have to access the configuration object everywhere and do that for multiple beans – Marian Klühspies Jan 09 '23 at 15:19