Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests assertions #379

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions force-app/main/default/classes/GeocodingServiceTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Generic unit tests for geocoding service
* @author Lorenzo
* @version 1.0
*/

@isTest
private with sharing class GeocodingServiceTest {
private static final String STREET = 'Camino del Jueves 26';
private static final String CITY = 'Armilla';
private static final String POSTAL_CODE = '18100';
private static final String STATE = 'Granada';
private static final String COUNTRY = 'Spain';
private static final Decimal LATITUDE = 3.123;
private static final Decimal LONGITUDE = 31.333;
private static final Decimal CONTINENT = '31.333';

@isTest
static void successResponse() {
// GIVEN
GeocodingService.GeocodingAddress address = new GeocodingService.GeocodingAddress();
address.street = STREET;
address.city = CITY;
address.postalcode = POSTAL_CODE;
address.state = STATE;
address.country = COUNTRY;

Test.setMock(
HttpCalloutMock.class,
new OpenStreetMapHttpCalloutMockImpl()
);

// WHEN
List<GeocodingService.Coordinates> computedCoordinates = GeocodingService.geocodeAddresses(
new List<GeocodingService.GeocodingAddress>{ address }
);

// THEN
Assert.areEqual(1, computedCoordinates.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Best Practice: Uncommented assertion

Severity

Warning

Finding

This assertion does not include any comments. Please add a comment that describes rationale of the assertion.

Why is this a problem?

Assertions let you test the behaviour of your application, increasing your confidence that your logic works as expected. Adding precise assertions to your tests is one of the most effective ways to detect and correct bugs. Using comments to describe the rationale of each assertion has the added benefit of documenting the inner working of the application logic, enhancing maintainability.

Help and documentation

Assert.areEqual(LATITUDE, computedCoordinates[0].lat);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Best Practice: Uncommented assertion

Severity

Warning

Finding

This assertion does not include any comments. Please add a comment that describes rationale of the assertion.

Why is this a problem?

Assertions let you test the behaviour of your application, increasing your confidence that your logic works as expected. Adding precise assertions to your tests is one of the most effective ways to detect and correct bugs. Using comments to describe the rationale of each assertion has the added benefit of documenting the inner working of the application logic, enhancing maintainability.

Help and documentation

Assert.areEqual(LONGITUDE, computedCoordinates[0].lon);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Best Practice: Uncommented assertion

Severity

Warning

Finding

This assertion does not include any comments. Please add a comment that describes rationale of the assertion.

Why is this a problem?

Assertions let you test the behaviour of your application, increasing your confidence that your logic works as expected. Adding precise assertions to your tests is one of the most effective ways to detect and correct bugs. Using comments to describe the rationale of each assertion has the added benefit of documenting the inner working of the application logic, enhancing maintainability.

Help and documentation

}

static void testMethod blankAddress() {
// GIVEN
GeocodingService.GeocodingAddress address = new GeocodingService.GeocodingAddress();

Test.setMock(
HttpCalloutMock.class,
new OpenStreetMapHttpCalloutMockImpl()
);

// WHEN
List<GeocodingService.Coordinates> computedCoordinates = GeocodingService.geocodeAddresses(
new List<GeocodingService.GeocodingAddress>{ address }
);

// THEN
Assert.areEqual(1, computedCoordinates.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Best Practice: Uncommented assertion

Severity

Warning

Finding

This assertion does not include any comments. Please add a comment that describes rationale of the assertion.

Why is this a problem?

Assertions let you test the behaviour of your application, increasing your confidence that your logic works as expected. Adding precise assertions to your tests is one of the most effective ways to detect and correct bugs. Using comments to describe the rationale of each assertion has the added benefit of documenting the inner working of the application logic, enhancing maintainability.

Help and documentation

Assert.isNull(computedCoordinates[0].lat);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Best Practice: Uncommented assertion

Severity

Warning

Finding

This assertion does not include any comments. Please add a comment that describes rationale of the assertion.

Why is this a problem?

Assertions let you test the behaviour of your application, increasing your confidence that your logic works as expected. Adding precise assertions to your tests is one of the most effective ways to detect and correct bugs. Using comments to describe the rationale of each assertion has the added benefit of documenting the inner working of the application logic, enhancing maintainability.

Help and documentation

Assert.isNull(computedCoordinates[0].lon);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Best Practice: Uncommented assertion

Severity

Warning

Finding

This assertion does not include any comments. Please add a comment that describes rationale of the assertion.

Why is this a problem?

Assertions let you test the behaviour of your application, increasing your confidence that your logic works as expected. Adding precise assertions to your tests is one of the most effective ways to detect and correct bugs. Using comments to describe the rationale of each assertion has the added benefit of documenting the inner working of the application logic, enhancing maintainability.

Help and documentation

}
@isTest
static void errorResponse() {
// GIVEN
GeocodingService.GeocodingAddress address = new GeocodingService.GeocodingAddress();
address.street = STREET;
address.city = CITY;
address.postalcode = POSTAL_CODE;
address.state = STATE;
address.country = COUNTRY;

Test.setMock(
HttpCalloutMock.class,
new OpenStreetMapHttpCalloutMockImplError()
);

// WHEN
List<GeocodingService.Coordinates> computedCoordinates = GeocodingService.geocodeAddresses(
new List<GeocodingService.GeocodingAddress>{ address }
);

// THEN
Assert.areEqual(1, computedCoordinates.size(), 'Asserts that the number of coordinates is equal to 1.');
Assert.isNull(computedCoordinates[0].lat, 'Assert that the first coordinate is null.');
Assert.isNull(computedCoordinates[0].lon, 'Assert that the first coordinate is null.');
}

public class OpenStreetMapHttpCalloutMockImpl implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('[{"lat": ' + LATITUDE + ',"lon": ' + LONGITUDE + '}]');
res.setStatusCode(200);
return res;
}
}

public class OpenStreetMapHttpCalloutMockImplError implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatusCode(400);
return res;
}
}
}