0

I am struggling with covering this if-else conditional.

public void pullIDAnalyticsReportAPEX() {
    crsUpdate(); 
    generateJSON();
}
public String generateJSON() {
    currentPgId = ApexPages.currentPage().getParameters().get('id');
    serializedData = new List<Contact>();
    credRevId = new List<Credit_Review__c>();
    homeAddress1 = new List<FPC_Address__c>();
    guarantorId1 = [SELECT PG1__c FROM Credit_Review__c WHERE Id = :currentPgId].PG1__c;

    if (guarantorID1 == NULL) {
        String nulLReturn = 'Null Data';
        return nullReturn;
    } 
    else {
        credRevId = [SELECT Id FROM Credit_Review__c WHERE Id = :currentPgId];
        serializedData = [SELECT Id, FirstName, LastName, Birthdate, SSN_Encrypted__c, SSN__c, MailingAddress, HomePhone, MobilePhone, Email FROM Contact WHERE Id = :guarantorId1];
        homeAddress1 = [SELECT Address_1__c, City__c, State__c, Zip_Code__c FROM FPC_Address__c WHERE Location_Type__c = 'Home Address' AND Contact__r.Id = :guarantorId1 ORDER BY CreatedDate DESC LIMIT 1];

        jk = new AccountWrapper(serializedData[0], credRevId[0], homeAddress1[0]);
        NewJSONString = JSON.serialize(jk);
        return NewJSONString;
    }
}

I am able to get through the if statement but not the else part. I have tried defining guarantorID1 with a hard coded value, queried for the ID, pulled it from a test contact, system.aserts, and other variations of these. In my test class I created an account, an opportunity, a credit review, a contact and tied all of these together. It is to my understanding that when you create a test-contact, it will be assigned an ID. I don't understand why I can't assign the contact ID as the PG1__c ID like so:

@isTest
static testmethod void firstTestAccount(){
    //no PG1__c ID
    Account a = new Account(Name = 'Test Account');
    insert a;
    Opportunity o = new Opportunity(AccountId = a.Id, Name = 'IDAnalyticsName', CloseDate = Date.today(), StageName = 'Prospecting');
    insert o;
    Contact c = new Contact(FirstName = 'FirstTest', LastName='LastTest', SSN_Encrypted__c ='000000000', HomePhone='7894561333', MobilePhone='9885556666', Email='test@test123.com', AccountId = a.Id);
    insert c;
    system.debug('this is the contact ID:' + c.Id); 
    Credit_Review__c cr = new Credit_Review__c(Account__c = a.Id, Opportunity__c = o.Id); //**NOTE- THERE IS NO PG1__C IN HERE this is how I am able to get through the first part of the conditional**
    insert cr;
    system.debug('this is cr.id' + cr.Id);
    Consumer_Risk_Summary__c crs = new Consumer_Risk_Summary__c(Error_Message__c = 'ID Analytics encountered an error while processing the request.', Credit_Review__c = cr.Id);
    insert crs;
    system.debug('this is crs credit review' + crs.Credit_Review__c);

    //PG1__c ID is available
    Account a2 = new Account(Name = 'Test Account 123');
    insert a2;
    Opportunity o2 = new Opportunity(AccountId = a2.Id, Name = 'IDAnalyticsName22', CloseDate = Date.today(), StageName = 'Prospecting');
    insert o2;
    Contact c2 = new Contact(FirstName = 'FirstTest22', LastName='LastTest22', SSN_Encrypted__c ='000000000', HomePhone='7894561333', MobilePhone='9885556666', Email='test@test123.com', AccountId = a2.Id);
    insert c2;
    system.debug('this is the contact ID:' + c.Id);
    Credit_Review__c cr2 = new Credit_Review__c(Account__c = a.Id, Opportunity__c = o.Id, PG1__c = c2.Id); //**NOTE THERE IS A PG1__C IN THIS CREDIT REVIEW which should allow me to enter into the second half of the if/else**
    insert cr2;
    system.debug('this is cr.id2' + cr2.Id);
    Id guarantorId1 = [SELECT PG1__c FROM Credit_Review__c WHERE Id = :cr2.Id].PG1__c;
    system.assertNotEquals(null, guarantorId1);
    Consumer_Risk_Summary__c crs2 = new Consumer_Risk_Summary__c(Credit_Review__c = cr2.Id);
    insert crs2;
    system.debug('this is crs credit review' + crs2.Credit_Review__c); 
}
  • Might you have a trigger on Credit_Review__c that clears out the value? – Adrian Larson Feb 14 '17 at 22:27
  • 1
    Where are you calling generateJSON in your test class? And you would need to pass record id to url query parameter. – Raul Feb 14 '17 at 22:29
  • @AdrianLarson no triggers on Credit_Review__c. I have also tried to clear out the values in the test class and add them back in but no success with that attempt either. – Olivia Feb 14 '17 at 22:32
  • @RahulSharma I am calling generateJSON when I call pullIDAnalyticsReportAPEX(). I will update my question with more code so you can see where I am calling it. – Olivia Feb 14 '17 at 22:33
  • do you set pageParameters ID=cr2.Id in your testmethod? – cropredy Feb 15 '17 at 0:50
1

None of the data you set matters during the test when you are basing the query:

guarantorId1 = [SELECT PG1__c FROM Credit_Review__c WHERE Id = :currentPgId].PG1__c;

On the currentPgId

currentPgId = ApexPages.currentPage().getParameters().get('id');

in fact, I do not understand how the query is not throwing an error (List has no rows for assignment to SObject) as currentPgId is null and you are attempting to pull a field from it

To set the value according to the code you have posted you need to set the Id URL parameter for the current page reference

PageReference pr = Page.[YOURPAGENAME];
pr.getParameters().put('id', guarantorId1);
test.setCurrentPage(pr);

Then when your test executes it will have the appropriate parameters set and get the correct record and enter the else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.