+ Start a Discussion
RustyboyRustyboy 

How to overcome "Too many DML statement" when testing

I have written a test case which is attempting to get maximum coverage on a complex page. I now keep hitting the "Too many DML statements" problem. I get that what this is for, but I need to find a way of working around this for testing purposes.

 

Any ideas?

 

NB: I could resort to breaking my test into multiple classes, but that seems messy!

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You can have more than one test method per test class. One particularly involved piece of code I had written had 16 test methods to cover it 100%; this is the most extreme example, of course. It's always possible to cover the code, but sometimes you have to get creative.

All Answers

Satyendra RawatSatyendra Rawat

Hi,

 

Please cross-check the apex code and trigger code make sure any DML operation not in loop,

and modify  in the testmethod make block using Test.starttest() and test.Stoptest();

 

 

 

digamber.prasaddigamber.prasad

Hi,

 

You should use Test.startTest() and Test.stopTest() to overcome this problem. Please go through below URL, you will find it helpful.

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

Let me know if you have any specific question.

 

Happy to help you!

RustyboyRustyboy

Thanks, but that is not the problem. I HAVE used Test.startTest() and Test.stopTest().

 

The problem is that of necessity I am making more than 150 DML calls to get the coverage. 

 

Incidentally, it works fine when I break it into 2 separate classes, but I would rather not do this if I can avoid it.

digamber.prasaddigamber.prasad

Hi, 

 

In that case, I guess you have to use 2 methods to achieve this.

sfdcfoxsfdcfox
You can have more than one test method per test class. One particularly involved piece of code I had written had 16 test methods to cover it 100%; this is the most extreme example, of course. It's always possible to cover the code, but sometimes you have to get creative.
This was selected as the best answer