I would highly recommend that you take a look through the User's Guide (
https://twitter.github.io/finatra/user-guide/) and the myriad of tests (as they are a great example of how to do things).
The simple answer to your question is you just instantiate the class, e.g. in your test you write:
new FirebaseClient(...) and pass values for the constructor args.
Which yes, means you need to create an HttpClient and a mapper, etc. This is normal instance instantiation -- nothing special going on here. The
@Singleton annotation is information for the injector to use about how it should construct an instance for you when asked. Thus you would need to use an injector.
Since you asked about "how to get instances of singleton classes", then I'm guessing you're asking about how to use an injector since you need to construct an object graph then ask the injector for a @Singleton annotated instance.
Again, this is all documented in the User's Guide and these are central concepts to the framework. It also really helps to understand
TwitterServer (since Finatra ties an object graph to a TwitterServer server) and the basics of
Finagle.
We'll take the Twitter Clone feature test as a starting place:
Thus in tests, there are two ways to create an object graph. Like the TwitterCloneFeatureTest you can create a server and then get a reference to it's in injector, e.g.,
class TwitterCloneFeatureTest
extends FeatureTest
with Mockito {
override val server = new EmbeddedHttpServer(new TwitterCloneServer)
val firebaseClient = server.injector.instance[FirebaseClient]
Taking the test as a starting point, if you wanted a reference to the firebaseClient, you would ask the embedded server for a handle to the injector of the server it's wrapping. Then ask the injector for an instance of the FirebaseClient from the object graph. Since the FirebaseClient class is annotated with @Singleton, every time you repeat: server.injector.instance[FirebaseClient], it'll be the same singleton instance.
So in a test, you could do this:
val injector = TestInjector(FirebaseClientModule)
val firebaseClient = injector.instance[FirebaseClient]
Thanks!
-c