Provide a handy method to check if CredentialBootstrap is initialized or not
jiurchuk opened this issue · 2 comments
Provide initialization check
In order to access to a Credential we need to have CredentialBootstrap
initialized.
In the implementation of CredentialBootstrap.initialize
it does:
if (privateCredentialDataSource != null) {
throw IllegalStateException("Credential bootstrap was already initialized.")
}
...
Variable privateCredentialDataSource
is private so we can't check it before running the initialization.
As I understand, the only way to handle this is with try/catch
or runCatching
or similar.
Can we provide a handy way to make this more elegant and not so exception-driven?
New or Affected Resource(s)
CredentialBootstrap initialization flow
Provide a documentation link
No response
Additional Information?
No response
Hi @jiurchuk,
The initialization call is ideally performed inside the Application class of the app, for example in the SampleApplication class found in the sample app in this repo. The readme doesn't provide suggestions on doing this however, and that can definitely be made clearer.
Ideally, initialize is placed somewhere that is guaranteed to only be called once, and developers should not have to worry about handling the exception. Adding a way to handle this without an exception (for example by returning a sealed class) might make the user think they need to handle the different results of CredentialBootstrap.initialize, but that is not the intention.
I still think this poses a problem when writing unit tests because CredentialBootstrap.initialize(mockCredentialDataSource)
can't be called in the @Before
function since this will cause it to be initialized multiple times. Also CredentialBootstrap.initialize(mockCredentialDataSource)
can't be called in the init
block of the class since at that point, mockCredentialDataSource hasn't been created yet. And lastly, it can't be put inside the individual @Test
functions since it will be called multiple times as well.