Context Configuration in spock test Context Configuration in spock test spring spring

Context Configuration in spock test


Was solved by:

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = Application.class)@WebAppConfiguration@IntegrationTest

and usage of RestTemplate

as in this question


Expanding on this since the bounty wanted some elaboration: Spring doesn't wire up beans by default in unit tests. That's why those annotations are needed. I'll try to break them down a little bit:

  • Uses SpringBootContextLoader as the default ContextLoader when no specific @ContextConfiguration(loader=...) is defined.
  • Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.

Without these annotations, Spring doesn't wire up the beans necessary for your test configuration. This is partially for performance reasons (most tests don't need the context configured).