0

I tried so much time to get the code coverage of my class more then 75% but i am getting error (like this : Methods defined as TestMethod do not support Web service callouts, test skipped) in production organization.

The same class and same test class i have wrote in sandbox and it was working fine and get me 93% code coverage but when i am doing this same process in production i am getting error (Methods defined as TestMethod do not support Web service callouts, test skipped).

so what is the reason behind this ?

@isTest
Public class GenereatControllerTest{

static testMethod void test(){

    Test.isRunningTest();

    Account acc=new Account();
    acc.Name='test';
    insert acc;

    Vendor_Order__c vo=new Vendor_Order__c();
    vo.Vendor__c=acc.id;
    insert vo;

    Attachment atta = new Attachment();
    atta.Body=Blob.valueOf('ww');
    atta.Name=vo.Name+' '+Date.today().year()+Date.today().month()+Date.today().day()+'_V'+(1)+'.pdf';  
    atta.ParentId=vo.Id;
    insert atta;

    Document doc=new Document();
    doc.Name=atta.Name;
    doc.FolderId=userinfo.getUserId();
    doc.Body=atta.Body;
    insert doc;

    PageReference pc = new PageReference('/apex/GenerateController?typ=send&id='+vo.id);
    Test.setCurrentPage(pc);

    GenerateController p = new GenerateController();
    p.id=vo.Id;
    p.getId();    
    p.init();
    ApexPages.currentPage().getParameters().put('typ','save');
    p = new GenerateController();
    p.id=vo.Id;
    p.getId();    
    p.init();
}

}

Nihar
  • 1,182
  • 2
  • 13
  • 30
  • If you are firing a callout in a test class, this error will come . You need to implement a mock. Can you post your test class ? – Amit Bangad Jul 20 '15 at 10:19
  • Please see my edited question for test class code – Nihar Jul 20 '15 at 10:22
  • Do you have a callout in any of the method of corresponding controller ? May be using Webservice methods or http request? – Amit Bangad Jul 20 '15 at 10:24
  • No. I am not using any kind of http request or Webservice methods. – Nihar Jul 20 '15 at 10:29
  • Check my answer. probably it will give you a way out. – Amit Bangad Jul 20 '15 at 10:33
  • Hey, but may i know why this is happened in only production not in sandbox (sandbox - 93% code coverage & production has error) ? – Nihar Jul 20 '15 at 10:36
  • because, the only rational answer is that particular critical update is not yet applied in sanbox but it was applied in production. Can you search in 'Quick Find' for critical updates and check ? – Amit Bangad Jul 20 '15 at 10:37

2 Answers2

1

you may want to include test.isrunningtest in your actual callout class to check you are not going to run actual callout and only test mock. take a look on this thread

Testing HttpCallout with HttpCalloutMock and UnitTest Created Data

Arjun Khatri
  • 1,338
  • 8
  • 16
1

I strongly believe this has to do with getContent() and getContentAsPDF() method. This was a critical update from salesforce which seems to be activiated in your production and not in sandbox so test class work fine in sandbox but not in production. This update basically considers the above two methods as callouts as opposed to regular method before. So the only solution I can think of is try implementing a Mock as opposed to these methods or just apply check of

if(!test.isrunningtest())

to ensure that these methods are called only in regular context and not in context of test class.

enter image description here

Amit Bangad
  • 3,890
  • 1
  • 15
  • 19
  • So, You mean i have to set that condition in test class like below : @isTest Public class GenereatControllerTest{

    if(!test.isrunningtest()){

    // My test class code

    }

    }

    – Nihar Jul 20 '15 at 10:44
  • First of all are you using any of these 2 methods i highlighted? – Amit Bangad Jul 20 '15 at 10:45
  • Yes. I am using getContentAsPDF() – Nihar Jul 20 '15 at 10:45
  • So now you CANNOT directly call that method in your test class context. You will have to either implement a mock or apply the check in original class (not test class) as I mentioned in my Answer above. If you need more inputs please post your method too. – Amit Bangad Jul 20 '15 at 10:53
  • Amit I have done. Thank you so much for your great valuable time and Answer. – Nihar Jul 20 '15 at 11:17