Context Configuration in spock test
Was solved by:
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = Application.class)@WebAppConfiguration@IntegrationTest
and usage of RestTemplate
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:
- @IntegrationTest (which is now deprecated in favor of @SpringBootTest ) from the docs:
- 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.
- @WebAppConfiguration adds the application context, and must be used with...
- @ContextConfiguration actually determines how to load the "class-level" metadata.
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).