Skip to content

OBPIH-6428 autowire api clients in tests#4630

Merged
ewaterman merged 2 commits intodevelopfrom
feature/OBPIH-6428-autowire-api-clients-in-tests
May 30, 2024
Merged

OBPIH-6428 autowire api clients in tests#4630
ewaterman merged 2 commits intodevelopfrom
feature/OBPIH-6428-autowire-api-clients-in-tests

Conversation

@ewaterman
Copy link
Member

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.

@ewaterman ewaterman self-assigned this May 20, 2024
* https://docs.spring.io/spring-framework/reference/testing/testcontext-framework.html
*/
@TestConfiguration
@ComponentScan("org.pih.warehouse.component.*")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core change of the PR. This scans all the test directories and adds the @TestComponent beans to the context

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file had a spelling error so I renamed it. That's why it looks like it's new

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ewaterman ewaterman requested review from awalkowiak and jmiranda May 20, 2024 23:32
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we pull the new config in

Copy link
Member

@jmiranda jmiranda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ewaterman ewaterman merged commit 4df749c into develop May 30, 2024
@ewaterman ewaterman deleted the feature/OBPIH-6428-autowire-api-clients-in-tests branch May 30, 2024 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants