3

I need Opportunity record to be inserted in test class and then use that Id in webservice call out. Tried every different combination but it asks me commit the DML operation before doing webservice call out and test class fails. Is there any standard way of data creation before doing webservice callout test ?

Here is my code:

@isTest
public class className_Test{

public static Opportunity doDML()
{
    //Logic to insert required records
}

 public static testMethod void  TestInterfaceCallout()
{
    Opportunity opp = doDML(); 
   //Mock implementation called 
   Test.setMock(WebServiceMock.class, new TestInterface_MockImpl());

   Test.startTest();
   WebserviceCls.WebServiceMethod(opp.Id);
   Test.stopTest();
}

Is there anything I am missing or is that impossible to do ?

2
  • Have you tried making the Opportunity a static variable outside of the testMethod? Like static Opportunity opp = doDML();? Commented Oct 17, 2014 at 20:36
  • Did you ever find a solution for this? Commented May 7, 2015 at 4:50

2 Answers 2

1

Actually you can set up data prior to making the callout but you have to wrap the setmock inside the test.starttest();

Wrapping it in the test.starttest starts a new transaction so you will not get the error. NOTE This still does not work if you are executing a batch inside the block. If it is a batch you will have to call the start, execute, and finish methods separately.(Still a bug)

Like so:

public static testMethod void  TestInterfaceCallout()
{
    Opportunity opp = doDML(); 

   Test.startTest();
   //Mock implementation called 
   Test.setMock(WebServiceMock.class, new TestInterface_MockImpl());
   WebserviceCls.WebServiceMethod(opp.Id);
   Test.stopTest();
}
0

You can't do a web service callout after a DML operation in the same transaction. try this code:-

@isTest
public class className_Test{

public static Opportunity doDML()
{
//Logic to insert required records
}

public static testMethod void  TestInterfaceCallout()
{
  Opportunity opp = doDML(); 

   Test.startTest();
  Test.setMock(WebServiceMock.class, new TestInterface_MockImpl());
  WebserviceCls.WebServiceMethod(opp.Id);
  Test.stopTest();
}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .