0

I have the following method and my code coverage tells me that none of the "if" conditions are being tested. I have written many tests trying to test for them, which pass, but still offer no code coverage. Any suggestions on a test that should allow me to pass any of the if conditions?

String[] listOfNames = new String[] {'name1', 'name2'};
public void doThis(){
    for(String name in listOfNames){
        for(Opportunity o : [SELECT prop1, prop2 FROM Opportunity WHERE Id =: opportunityId]){
            // no code coverage
            if(name == 'name1'){
                o.prop1 = 'this';
                o.prop1 = 'that';
            }
            if(name == 'name2'){
                o.prop1 = 'this';
                o.prop1 = 'that';
            }
            // end no code coverage
            update o;
        }
    }
}

Everything in this method is tested except the if conditions and the updating of the properties. (Everything inside the //no code coverage is not "testable" everything else, is covered, the loop, the SOQL, the Update statement, etc.)

1
  • And your test code? Also, where are you setting the opportunityId. Basically your query in the for loop is not returning any records thus never actually enters the second for block – Eric Nov 20 '15 at 14:23
2

First: change:

for(String name : listOfNames){

to

for(String nm : listOfNames){

and adjust the rest of your code as name may be causing issues

Your code does not make sense as the final result will always be o.prop1 = 'that'. There is no need for the second if, etc. Assuming you are just showing a test code fragment (I hope so)

Also, where are you setting the opportunityId. Basically your query in the for loop is not returning any records thus never actually enters the second for block.

With that and the unknown name or the hows and whats the following will be a guide for you

@isTest
private class myUnitTest{

   private static test method void basicTest(){

      Opportunity o = New Opportunity(Name = 'Test opportunity');
      insert o;

      MYUNKNOWNCLASS b = New MYUNKNOWCLASS();
      b.opportunityId = o.id;
      b.doThis;

      o = [Select prop1__c From Opportunity Where ID = :o.id];
      system.assertEquals('that',o.prop1__c); //will always be set to that based on your code

   }

}
6
  • ... final result will always be o.prop1 = 'that' ... – sfdcfox Nov 20 '15 at 16:18
  • @sfdcfox - yup, I was thinking one thing and typed another (see code) – Eric Nov 20 '15 at 16:41
  • Since when is for/in valid Apex? It should be :, no? – Adrian Larson Nov 20 '15 at 17:59
  • @AdrianLarson - Feel free to edit if you find that a copy and paste of the OP's code is incorrect - I should have caught that (not have tunnel vision) so thanks for pointing it out – Eric Nov 20 '15 at 18:31
  • @Eric Makes sense, the etiquette on editing an answer is still a bit murky for me so I err on the side of leaving it alone. – Adrian Larson Nov 20 '15 at 18:32

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.