Reputation: 814
I'm calling a third party endpoint so to speak. I just call the endpoint and parse the response.
public ListUsers listUsers() {
String url = "/api/v1/account/?apikey=" + apiKey;
String signature = null;
try {
signature = generateSignature(url);
} catch (SignatureException e) {
e.printStackTrace();
}
Call newCall = apiCall("GET", url, signature, null);
Type localVarReturnType = new TypeToken<ListUsers>() { }.getType();
ApiResponse<ListUsers> responseFromApiClient = apiClient.execute(newCall, localVarReturnType);
return responseFromApiClient.getData();
}
listUsers() func has been refactored for better abstraction
public ListUsers listUsers() {
String url = "/api/v1/account/?apikey=" + apiKey;
Type localVarReturnType = new TypeToken<ListUsers>() { }.getType();
ApiResponse<ListUsers> responseFromApiClient = apiClient.execute(buildApiCall("GET", url, null), localVarReturnType);
return responseFromApiClient.getData();
}
This particular endpoint does not take in any body, and returns json that I then parse.
{
"meta": {
"limit": 0,
"offset": 0,
"total_count": 1
},
"objects": [
{
"address_one": "",
"address_three": "",
"address_two": "",
"country_code": "",
"cs_domain_id": null,
"date_joined": "2019-07-05T13:50:21",
"disable_personal_account": false,
"email": "[email protected]",
"first_name": "",
"id": 1,
"is_reseller": true,
"is_superuser": true,
"is_verified": true,
"last_login": null,
"last_name": "",
"paywall": true,
"postal_code": "",
"projects": [],
"reference_number": "",
"reseller_id": 1,
"reseller_name": "ROOT",
"reseller_rights": [
{
"domain_id": null,
"id": 1,
"level": "owner",
"name": "ROOT"
}
],
"resource_uri": "/api/v1/account/1/",
"saml": false,
"state": 0,
"status": "enabled",
"surname": "",
"username": "admin",
"uuid": "1c208540-3ed9-4741-a936-a574a3ded12a"
}
]
}
And then this json response get parsed.
How do I write a proper Unit test for this, or is it not needed? The only test I have come up with, is just a basic test where I actually call the service, but as for pure unit test that is not gonna work on my build server. Here is the test
@Test
void getListProjectsTest() {
GqConsoleApiClient gcac = new GqConsoleApiClient();
ListProjects response = gcac.listProjects();
System.out.println(listProjects);
Assert.assertEquals(20, response.getMeta().getLimit());
Assert.assertEquals("some-customer-id", response.getObjects().get(0).getCustomerId());
}
Upvotes: 3
Views: 4966
Reputation: 131326
How do I write a proper Unit test for this, or is it not needed?
In terms of unit tests, your logic is quite terse.
You call the API :
ApiResponse<ListUsers> responseFromApiClient = apiClient.execute(newCall, localVarReturnType);
And in the next statement :
return responseFromApiClient.getData();
you simply return getData()
.
So in terms of unit testing, what you need is mocking the return of execute()
with any ApiResponse<ListUsers>
object and assert that you return getData()
from that object.
Something like that :
// GIVEN
ApiResponse<ListUsers> responseFromApiClientByMock = Mockito.any();
Data dataByMock = Mockito.when(responseFromApiClientByMock.getData()).thenReturn(Mockito.mock(Data.class);
Mockito.when(apiClientMock.execute(newCall, localVarReturnType)).thenReturn(responseFromApiClientByMock);
// WHEN
Data actualData = foo.listUsers();
// THEN
Assertions.assertSame(dataByMock, actualData);
Of course that kind of component to test produces very shallow unit tests.
So you should definitively create integration tests, for the listUsers()
method or maybe at a higher level method because you don't want to duplicate integration test logic in each layer.
You can refer to the pvpkiran answer that addresses well the integration test question and make it to start at the right level : listUsers()
or maybe higher.
Upvotes: 1
Reputation: 27018
There are multiple ways to do this. If I were you, I would do it this way
Firstly create a json file with the proper response
Write a test case in which
2.1. Load the json file in the test class
File sampleFile = new File(URLDecoder.decode(classLoader.getResource("jsonFilePath").getFile(), "UTF-8"));
2.2 Convert this File into java type which would be returned from the third party API. In your case it is ApiResponse<ListUsers>
. For this you can use Jackson or Gson libraries
2.3 You should create a mock of apiClient type and mock the execute
call. And return the java object got from step 2.2. Something like this
when(apiClient.execute(any(), any()).thenReturn(....)
2.4 write asserts to check various aspects of the java objects
This way you can easily create multiple json files to test various responses from the api.
Upvotes: 3