0
global static String saveSignature(String signatureBody, String parentId) 
{
    try
    {
        //parentId = System.currentPageReference().getParameters().get('Id');
        system.debug('Record Id == ' + parentId);
        system.debug('Record IdTourId == ' + TourId);
        system.debug(signatureBody);
        Attachment a = new Attachment();
        a.ParentId = parentId;
        a.Body = EncodingUtil.base64Decode(signatureBody);
        a.ContentType = 'image/png';
        a.Name = 'LeadGuest Signature.png';
        insert a;
        return '{success:true, attachId:' + a.Id + '}';
    }catch(Exception e)
    {
        return JSON.serialize(e);
    }
    return null;
}

I have tried below code :-

static testmethod void saveSignaturetest(){


   Tour_Reservation__c tr2 = new Tour_Reservation__c(Tour_Reservation_Source__c='VIP Sales');
    insert tr2;
    Tour__c tObj1 = new Tour__c(Tour_Reservation__c=tr2.Id);
    insert tObj1;
   string before = 'Testing base 64 encode';
    Blob beforeblob = Blob.valueOf(before);
    string paramvalue = EncodingUtil.base64Encode(beforeblob);
    Blob afterblob = EncodingUtil.base64Decode(paramvalue);

    Attachment att = new Attachment(ParentID=tObj1.Id, Body =  afterblob,Name = 'Test Attachment');
    insert att;
    test.startTest();
    VipTourListComponentController.saveSignature('paramvalue','tObj1.Id');
    insert att;
    test.stopTest();
}

Uncovered code :-

a.Body = EncodingUtil.base64Decode(signatureBody);
a.ContentType = 'image/png';
a.Name = 'LeadGuest Signature.png';
insert a;
return '{success:true, attachId:' + a.Id + '}';
  • 1
    Requests for help in code coverage generally don't receive much positive attention. However, in this case, I think this is a decent question. The relevant method being tested and the test class were provided, as were the lines not being covered. That made it really easy for me to find the issue. The question is also on a specific issue, and showed a good faith effort to resolve it (as opposed to most questions of this nature, which show little understanding of how coverage is gained, and less interest in learning how to test). As such, I've upvoted this question. – Derek F Jun 22 '17 at 16:44
2

Well, it looks like you're getting into your catch block in saveSignature() because the parentId that you're passing is literally the string 'tObj1.Id'.

That string isn't an Id, and trying to assign it to a.ParentId is causing an exception.

Solving this issue should be as simple as making the following change to your test method.

// This is wrong, and will cause an exception in your saveSignature method
//   because 'tObj1.Id' is a string literal that doesn't conform to the format
//   that Ids use.
//VipTourListComponentController.saveSignature('paramvalue','tObj1.Id');

// This is correct. Notice how I've removed the single quotes around tObj1.Id
// This will cause tObj1.Id to be evaluated before actually calling the method,
//   and end up passing the corresponding Id value to your method.
VipTourListComponentController.saveSignature('paramvalue',tObj1.Id);

+edit:

Of course, I would be remiss if I also didn't mention the lack of assertions in your test. Assertions are what makes a unit test truly useful, and a unit test without assertions isn't really a test (it's only a 'smoke' test, which only ensures that your code doesn't crash/throw an exception under a specific circumstance).

In this case, a good start would be to assert that the JSON you get from your saveSignature() method contains the substring 'success:true' (as is, there isn't a good way for you to know what the attachment Id will be).

Try to incorporate something like this into your test:

String jsonResult = VipTourListComponentController.saveSignature('paramvalue',tObj1.Id);

// assertEquals() and assertNotEquals() only require two parameters.
// The third parameter is a custom message to print when the assertion fails,
//   and including it is generally a good idea.
// It can be very helpful in determining exactly what went wrong (and help determine where
//   things went wrong) if this test fails at some point in the future.
System.assertEquals(true, jsonResult.contains('success:true, attachId:'), 'We expected a success message as the return value from saveSignature, but encountered something else. Got ' + jsonResult);
  • thanks a lot, now code coverage is proper for the uncovered code. – amiya Jun 28 '17 at 14:26
  • test method is getting failed and the error thrown is :- System.JSONException: Apex Type unsupported in JSON: Object. Any thoughts on this?? – amiya Jun 29 '17 at 12:49
  • @amiya Not really. I haven't run into that error myself before. This is starting to get to the point where you should ask a new question, but you should search for similar questions before asking your own. For example, there is this question asking about that exact error message which I would suggest taking a look at. – Derek F Jun 29 '17 at 13:05

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.