My production code goes like this below:
try{
insert githhubUsersToInsert;
} catch (DmlException e){
throw new GithubUsersException('create record failed.');
}
The unit test report tells that the catch block is not entered (3rd and 4th lines), but I am so sure my tests covers this exception, thus I made one more test to add a System.Debug
to indicate the catch block is entered;
try{
insert githhubUsersToInsert;
} catch (DmlException e){
System.Debug('>>>> DMLException entered');
throw new GithubUsersException('create record failed.');
}
Now, report says 4th line (System.Debug
one) is covered, while the other 2 lines (above and below it) are not.
How is this possible?
==== full code version ====
The full code version is here in github: https://github.com/Xixiao007/soc-demo/commit/18b0d9e20dfc24b63efb5a3dc83c4b346706b46d
This is the DX command result:
❯ sfdx force:apex:test:run -cy -l RunAllTestsInOrg -w 5 -r human
=== Apex Code Coverage
ID NAME % COVERED UNCOVERED LINES
────────────────── ──────────────────── ───────── ───────────────
01p3E000001czhEQAQ GithubUserService 100%
01p3E000001czhFQAQ GithubUsers 89% 9,11
01p3E000001czhIQAQ RemoteService 100%
01p3E000001czhHQAQ JSONParser 100%
01p3E000001d1C5QAI TestData 100%
01p3E000001czhDQAQ GithubUserController 100%
01p3E000001czhGQAQ GithubUsersSelector 100%
=== Test Results
TEST NAME OUTCOME MESSAGE RUNTIME (MS)
────────────────────────────────────────────────────────────────────── ─────── ─────── ────────────
GithubUserControllerTest.whenLocalAlreadyExistThenCreateUserFail Pass 175
GithubUserControllerTest.whenRemoteExistAndLocalNonExistThenCreateUser Pass 50
GithubUserControllerTest.whenRemoteNonExistThenCreateUserFail Pass 23
=== Test Summary
NAME VALUE
─────────────────── ───────────────────────────────────────────────
Outcome Passed
Tests Ran 3
Passing 3
Failing 0
Skipped 0
Pass Rate 100%
Fail Rate 0%
Test Start Time Dec 27, 2018 6:36 PM
Test Execution Time 248 ms
Test Total Time 248 ms
Command Time 4545 ms
Hostname https://data-power-907.cs82.my.salesforce.com/
Org Id 00D3E000000DCMIUA4
Username test-7qg97nwljedl@example.com
Test Run Id 7073E000011TZqc
User Id 0053E000002tkJdQAI
Test Run Coverage 96%
Org Wide Coverage 96%