OBPIH-6428 autowire api clients in tests#4630
Conversation
| * https://docs.spring.io/spring-framework/reference/testing/testcontext-framework.html | ||
| */ | ||
| @TestConfiguration | ||
| @ComponentScan("org.pih.warehouse.component.*") |
There was a problem hiding this comment.
This is the core change of the PR. This scans all the test directories and adds the @TestComponent beans to the context
There was a problem hiding this comment.
This file had a spelling error so I renamed it. That's why it looks like it's new
There was a problem hiding this comment.
And this is what the change allows us to do. It's simple, but it saves us from having to init new XApi classes in each of our tests.
There was a problem hiding this comment.
These are components containing the default request specifications. It does common stuff like set the content-type and the session cookie so that we don't need to specify it for every single request.
We auto-wire them then in ApiSpec call loadContext to actually populate them since it needs to be done after the test user is authenticated.
| * a clean, known state for each test. | ||
| */ | ||
| @Integration | ||
| @Import(ComponentTestConfig.class) |
There was a problem hiding this comment.
This is where we pull the new config in
JIRA: https://pihemr.atlassian.net/browse/OBPIH-6428
Currently our API tests are initializing their own XApi classes. This is fine, but a better approach would be to annotate those Api classes with @TestComponent and then in the test, pull them in via @Autowired. This way we only have to initialize the classes once for the entire tests, and by using @TestComponent (as opposed to @component) we can guarantee that they’re only wired in for our tests.