Testing
Being resourceful
A crud API
We will start testing the basic CRUD API that will consume the JDBC DAO that we have seen before in the JDBC example. All DAO interactions must be mocked because we have tested before and we want to maintain the tests like unit ones (independent, small, fast to run, etc).
The test class we are talking about is: DropwizardJerseyExchangeResourceTests.java
You have to review these pieces of code for using Dropwizard tools for resource testing:
@ExtendWith(DropwizardExtensionsSupport.class)
and this one to:
@RegisterExtension
public static final ResourceExtension resources = ResourceExtension.builder() .addResource(new UVAExchangeResource(dao)).build();This is all Dropwizard, you have to put your attention in
The first one is for registering the base Dropwizard extension (for testing all components in the framework) and the second y for testing resources specifically. This ResourceExtension will provide you a mini server to start your resources and invoke them as if you were using http.
But where does Juamuahaca appear? You will have to register and extension and implement an interface
@RegisterExtension
public DropwizardResourceCrudExtension<UVAExchange> extension = new DropwizardResourceCrudExtension<UVAExchange>();
public interface HttpWebServiceDoubleHelper<T> {
List<T> mockSelectAllResult();
void mockRepositoryFindAllOk(List<T> mockedResult);
void mockRepositoryFindAllNotFound();
void mockRepositoryFindAllError();
T mockOne();
void mockRepositoryFindByIdOk(T mockedResult);
void mockRepositoryFindByIdNotFound(T mockedResult);
void mockRepositoryFindByIdError(T mockedResult);
void mockRepositoryUpdateOk(T mockedResult);
void mockRepositoryUpdateError(T mockedResult);
void mockRepositoryDeleteOk(T mockedResult);
void mockRepositoryDeleteError(T mockedResult);
void mockRepositoryCreateOk(T mockedResult);
void mockRepositoryCreateServerError(T mockedResult);
void mockRepositoryCreateUknownError(T mockedResult);
void mockRepositoryUpdateUknownError(T mockedResult);
void mockRepositoryDeleteUknownError(T mockedResult);
void mockRepositoryDeleteNotFoundError(T mockedResult)
This extension will give you a lot of methods and do all the work for you. You only have to implement the interface and invoke the right method as shown in the test class.
Pretty neat, as a bonus we build a custom assert for the response code in the request so you don't need to assert all the time. They are in the ResponseStatusAssert.java as static methods.
Please read and try to understand all the tests in the test class.
Some async
On the motivation page, we discuss a specific functionality: a web scraper for updating the UVA Exchange. We decided to build this as an async resource. It hasn't a Jumahuaca core helper because it is very particular for that functionality, so you have to pay attention to the example only.
There are two test classes: DropwizardJerseyScraperResourceWithoutScrapingTests.java and DropwizardJerseyScraperResourceWithScrapingTests.java. The difference between us that the first one would not scrap the website and the scraping part would be mocked (unit test) and the second one has integration tests.
As we said before Jumahuaca core it's not present. All the tools are there inside the Dropwizard extension. We encourage you to review all the test class but the more important code is this piece:
Future<Response> futureResponse = mockedResources.target(PREFIX+UVA_SCRAPER_POST_PATH).request().async().
post(Entity.entity(monthYear, MediaType.APPLICATION_JSON));
Response response = futureResponse.get();
Thread.sleep(ESTIMATED_TIME_TO_COMPLETE);
Last updated
Was this helpful?