dimanche 15 février 2015

Resource unit tests with Dropwizard 0.8.0-rc2

I want to test my Jersey resources in a Dropwizard 0.8.0-rc2 application. An additional obstacle is that I use TestNG instead of JUnit, so I have to do some things by hand that I copied from DropwizardClientRule.


Now I have some resources that are secured by @Auth. In Dropwizard 0.7.1 I added the authenticator to the test application as follows:



DropwizardResourceConfig resourceConfig = DropwizardResourceConfig.forTesting(…);
Authenticator<BasicCredentials, Identity> authenticator =
credentials -> getAuthorizedIdentity();
resourceConfig.getSingletons().add(
new BasicAuthProvider<>(authenticator, "TEST"));


Here, getAuthorizedIdentity() would fetch some test authorization. This would be passed to the @Auth annotated injected parameter. Now, of course, with Jersey 2 things have changed a bit. I tried:



DropwizardResourceConfig resourceConfig = DropwizardResourceConfig.forTesting(…);
Authenticator<BasicCredentials, Identity> authenticator =
credentials -> getAuthorizedIdentity();
resourceConfig.register(AuthFactory.binder(
new BasicAuthFactory<>(authenticator, "TEST", Identity.class)));


The effect is that I get 401 on all secured resources. Debugging shows that my authenticate function is never called at all! If I remove the registration, then I will not get 401s anymore (so the registration is not without effect), but now the secured resources are unsecured and get null for the @Auth annotated parameter.


So, how do I add the authenticator to the test context so it will work? The production code works fine with almost the same line.


Aucun commentaire:

Enregistrer un commentaire