Find your content:

Search form

You are here

Am I testing everything in my test class?

 
Share

I'm trying to get an idea as to whether there is anything I am perhaps missing from my test class?

Here is my apex class:

if(null==new_user) {
    return doErr(
        'new_user cannot be null. Must be a <String => String> map.','K001',result
    );
}
String user_name = new_user.get('user_name');

if(null==user_name || ''==user_name) {
    return doErr('new_user.user_name cannot be null or blank.','K002',result);
}

Contact ctc;
Id acct_id;

for(Contact ct:[
    select 
    Id,
    Name,
    AccountId,
    ParFX_Username__c,
    LastName,FirstName,
    Email,
    Platform__c,
    ParFX_Role__c 
    from Contact 
    where ParFX_Username__c=:user_name 
    limit 1]) { 
        //ParFX_Username__c field has 'unique' set
        ctc=ct;
        acct_id=ct.AccountId;
}
if(null!=ctc && ctc.Platform__c!='ParFX') {
    return doErr('new_user.user_name is unavailable: '+user_name,'K003',result);
}

Id art,crt;
for(RecordType rt:[
    SELECT 
        Id,
        DeveloperName 
    FROM RecordType 
    WHERE SobjectType='Account' AND DeveloperName='External'
]) {
    art=rt.Id;
}

for(RecordType rt:[
    SELECT 
        Id,
        DeveloperName 
    FROM RecordType 
    WHERE SobjectType='Contact' 
    AND DeveloperName='External'
 ]) {
    crt=rt.Id;
}

String acct_name;
if(null==acct_id 
        && null!=new_user.get('account') 
        && ''!=new_user.get('account')) {
    acct_name = new_user.get('account');
    for(Account a:[
        select 
            Id,
            Name 
        from Account 
        where RecordTypeId=:art 
        and Name=:acct_name 
        limit 1
    ]) {
        acct_id=a.Id;
    }
}
// At this stage we know whether the Contact and/or Account already exist.
if(null==acct_id) {
    Integer mx_ukn_an=0;
    for(Account a:[
        SELECT 
             id,
             Name 
        FROM Account 
        WHERE Name LIKE '<unknown %'
    ]) {
        if(a.Name.startsWith('<unknown ') && a.Name.length()>10) { 
            String  an = a.Name.substring(9).trim();
            an = an.removeEnd('>');
            Integer  an_n = 0;
            try { an_n = Integer.valueOf(an); } catch(TypeException e) {}
            if(an_n>mx_ukn_an) mx_ukn_an = an_n;
        }
    }
    mx_ukn_an++;
    Account ua = new Account(
        Name='<unknown'+mx_ukn_an.format()+'>',RecordTypeId=art
    );
    try { insert ua; } 
    catch (System.DmlException e) { 
        return doErr(
             'Error while inserting Account: '+e.getMessage(),'D001',result); 
    }
    acct_id = ua.Id;
}
if(null==acct_id) return doErr('Could not create new Account.','D002',result);

if(null==ctc) {
    String last_name = '<unknown last name>';
    if(null!=new_user.get('surname') 
            && ''!= new_user.get('surname')) {
        last_name = new_user.get('surname');
    }
    ctc = new Contact(
        ParFX_Username__c = user_name,
        RecordTypeId=crt,
        LastName=last_name,
        AccountId=acct_id,
        Platform__c='ParFX'
    );
    if(null!=new_user.get('email')) {
        ctc.Email = new_user.get('email');
    }
    if(null!=new_user.get('first_name')) {
        ctc.FirstName = new_user.get('first_name');
    }
    if(null!=new_user.get('role')) {
        ctc.ParFX_Role__c = new_user.get('role');
    }
    try { insert ctc; } 
    catch (System.DmlException e) {
         return doErr(
             'Error while inserting Contact: '+e.getMessage(),'D003',result);
    }
} else {
    if(null!=new_user.get('surname') 
             && ''!= new_user.get('surname')) {
        ctc.LastName = new_user.get('surname');
    }
    if(null!=new_user.get('email')) {
         ctc.Email = new_user.get('email');
    }
    if(null!=new_user.get('first_name')) {
         ctc.FirstName = new_user.get('first_name');
    }
    if( null!=new_user.get('role')) {
         ctc.ParFX_Role__c = new_user.get('role');
    }
    try { update ctc; } 
    catch (System.DmlException e) {
         return doErr(
             'Error while updating Contact: '+e.getMessage(),'D004',result);
    }      
}
if(null==ctc) return doErr('Could not create new Contact.','D005',result);

Opportunity o4c;
for(Opportunity op:[
    select 
        Id,
        Name,
        Web_Login_Provided_by_ParFX__c 
    from Opportunity 
    where AccountId=:ctc.AccountId 
    and Name='ParFX On-Boarding' 
    order by CreatedDate 
    limit 1
 ]) {
    o4c = op;
}
Id rtid;
If(ctc.ParFX_Role__c=='Founder'){
     rtid=[select id from recordType where name='Member'].id;
}
else if (ctc.ParFX_Role__c=='Member'){
     rtid=[select id from recordType where name='Member'].id;
}
else if (ctc.ParFX_Role__c=='ISV'){
     rtid=[select id from recordType where name='ISV'].id;
}
else if (ctc.ParFX_Role__c=='Prime Bank'){
     rtid=[select id from recordType where name='Prime Bank'].id;
}
else if (ctc.ParFX_Role__c=='Prime Client'){
     rtid=[select id from recordType where name='Prime Client'].id;
}
if(null == o4c) {
    Opportunity uo = new Opportunity(
         RecordTypeID=rtid,
         Name='ParFX On=Boarding',
         AccountId=ctc.AccountId,
         StageName='Prospect',
         PrimaryContact__c=ctc.Id,
         CloseDate=Date.today(),
         Web_Login_Provided_by_ParFX__c=Date.today());
    try { insert uo; o4c = uo; } 
    catch (System.DmlException e) {
        return doErr(
            'Error while inserting Opportunity: '+e.getMessage(),'D006',result);
    }
} else {
    if(null==o4c.Web_Login_Provided_by_ParFX__c) {
        o4c.Web_Login_Provided_by_ParFX__c = Date.today();
        try { update o4c; } 
        catch (System.DmlException e) {
            return doErr(
                'Error while updating Opportunity: '
                + e.getMessage(),'D007',result
            );
        }
    }
}

Map<String,Object> user_res = new Map<String,Object>();
user_res.put('account',new_user.get('account'));
user_res.put('user_name',new_user.get('user_name'));
result.put('user',user_res);
result.put('success',1);
String result_str = JSON.serialize(result);
return result_str;

And here is the test class:

Account a = new Account(Name='Bank of Database Testing2');
insert a;
Contact ctc = new Contact(
     AccountId=a.Id,
     FirstName='Ted',
     LastName='Tester',
     ParFX_Role__c='Founder');
insert ctc ;
Opportunity op = new Opportunity(
    AccountId=a.Id,
    Name='ParFX On-Boarding',
    StageName='Prospect',
    CloseDate=Date.today(),
    Web_Login_Provided_by_ParFX__c=Date.today());
insert op ;

ParFX_NewUser.doPost((Map<String,String>)JSON.deserialize('{\"user_name\":\"adam.sheik\",\"email\":\"adam.sheik@somewhere.org\",\"first_name\":\"Adam\",\"surname\":\"Sheik\",\"role\":\"Analyst\",\"entity\":\"Entity 1\",\"branch\":\"Tokyo\",\"account\":\"<unknown 1>\"}',Map<String,String>.class),'' );
ParFX_NewUser.doPost((Map<String,String>)JSON.deserialize('{\"user_name\":\"buck.rogers\",\"email\":\"adam.sheik@somewhere.org\",\"first_name\":\"Adam\",\"surname\":\"Sheik\",\"role\":\"Analyst\",\"entity\":\"Entity 1\",\"branch\":\"Tokyo\"}',Map<String,String>.class),'' );
ParFX_NewUser.doPost((Map<String,String>)JSON.deserialize('{\"user_name\":\"adam.sheik\",\"email\":\"adam.sheik@somewhere.org\",\"first_name\":\"Adam\",\"surname\":\"Sheik\",\"role\":\"Analyst\",\"entity\":\"Entity 1\",\"branch\":\"Tokyo\",\"account\":\"Bank of Database Testing\"}',Map<String,String>.class),'' );
ParFX_NewUser.doPost((Map<String,String>)JSON.deserialize('{\"user_name\":\"adam.sheik\",\"email\":\"jeffrey.castro@somewhere.org\",\"first_name\":\"Adam\",\"surname\":\"Sheik\",\"role\":\"Analyst\",\"entity\":\"Entity 1\",\"branch\":\"Tokyo\",\"account\":\"Bank of Database Testing\"}',Map<String,String>.class),'' );

}

I'm stumped as to what I could be missing here. I've pretty much modified an existing apex class developed for us and wanting to push these changes into my Prod.

Thanks.


Attribution to: Jeff

Possible Suggestion/Solution #1

Using the developer console you can view the test coverage for each class, and when clicking on the class you can see the exact lines that are covered or not. It's explained nicely in this doc:

http://help.salesforce.com/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en_US

If you need help getting additional coverage, post that here as a separate question.


Attribution to: Guy Clairbois
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31625

My Block Status

My Block Content