+ Start a Discussion
jaw999jaw999 

test code for WebService assist please

Am having trouble getting coverage for the following webservice class - would like assistance

have some experience getting coverage on simpler classes and on triggers but am not finding coverage responding the same way to this webservcie.

thanks for any help!

 

Global class PersonCoverage {
    
    Webservice static String addCoveragePerson(Coverage_Person__c[] coverageList){
        
        List <Contact> contacts = new List<Contact>();
        List<Account> accounts = new List<Account>();
         List<User> users = new List<User>();
        List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
        String status = '';
        String message = '';
        List<Coverage_Person__c> missingCoverage = new List<Coverage_Person__c>();
        
        for(Coverage_Person__c coveragePerson:coverageList){
            
            Savepoint sv = Database.setSavepoint();
                
            accounts = [SELECT A.ID,A.NAME FROM Account A WHERE A.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
            contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1];
            users = [SELECT c.EMP_ID__c,c.Name FROM User c where c.EMP_ID__c = :coveragePerson.emp_id__c and IsActive=true limit 1];
            coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c and c.emp_id__c = :coveragePerson.emp_id__c limit 1 ];
        
            if(accounts.size() == 0 ){
            	 if(message == '')
            	 	message = 'No Inst for Account addPersonCoverage';
            	 message += coveragePerson;
            	 missingCoverage.add(coveragePerson);
            	 status = 'Error';
                //Notifier.DoNotification(' No Inst for Account addPersonCoverage::'+coveragePerson.aims_client_id__c,coveragePerson);
                //return 'ERROR: Missing client: '+aimsClientId ;
                continue;
            }
            
            if(contacts.size() == 0){
            	if(message == '')
            	 	message = 'No Contact for Account addPersonCoverage';
                message += coveragePerson;
                missingCoverage.add(coveragePerson);
                //return 'ERROR: Missing client: '+aimsClientId ;
                continue;
            }
            
            //if the coverage exists, then delete it first
            if(coverages.size() != 0){
            	Coverage_Person__c deleteCoveragePerson = coverages[0];
                try{
                    Database.delete(deleteCoveragePerson);    
                }catch(DmlException e){
                    Database.rollback(sv);
                    Notifier.DoNotification('PersonCoverage.deleteCoveragePerson: ERROR on deleteCoveragePerson: '+e.getDmlMessage(0), deleteCoveragePerson);
                } 
            }
            
            Account acct = accounts.get(0);
            Contact contact = contacts.get(0);
            if(users.size()!=0){
            	User user = users.get(0);
            	coveragePerson.User__c = user.Id;
            }
            
            coveragePerson.Account__c= acct.Id;
            coveragePerson.Contact__c = contact.Id;  
            coveragePerson.name = contact.name;
            try{
                Database.insert(coveragePerson);    
            }catch(DmlException e){  
                Database.rollback(sv);
                Notifier.DoNotification('PersonCoverage.addCoveragePerson: ERROR on Inserting Of Coverage: '+e.getDmlMessage(0), coveragePerson);
            }
            
             
        }
        
        if(status == 'Error'){
        	Notifier.DoCoverageNotification(' Missing data on addCoveragePerson:::',missingCoverage);
        }
        
        return message; 
    }
    
     Webservice static String deleteCoveragePerson(Coverage_Person__c[] coverageList){
     	
     	String message = '';
     	String status = '';
     	List<Coverage_Person__c> missingCoverages = new List<Coverage_Person__c>();
         
        List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
        
        for(Coverage_Person__c coveragePerson:coverageList){
            
            Savepoint sv = Database.setSavepoint();
                
            coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c limit 1 ];
        
            if(coverages.size() == 0 ){
            	status = 'Error';
            	if(message == '')
            		message = 'No Coverage for deleteCoveragePerson';
            	message += coveragePerson; 
            	missingCoverages.add(coveragePerson);
                //Notifier.DoNotification(' No Coverage for deleteCoveragePerson::'+coveragePerson.aims_coverage_id__c,coveragePerson);
                //return 'ERROR: Missing client: '+aimsClientId ;
            }else{  
                Coverage_Person__c deleteCoveragePerson = coverages[0];
                try{
                    Database.delete(deleteCoveragePerson);    
                }catch(DmlException e){
                    Database.rollback(sv);
                    Notifier.DoNotification('PersonCoverage.deleteCoveragePerson: ERROR on Deleting Of Coverage: '+e.getDmlMessage(0), deleteCoveragePerson);
                }   
            }
               
        }
        
        if(status == 'Error'){
       		 Notifier.DoCoverageNotification(' No Coverage for deleteCoveragePerson::',missingCoverages);
        }
        
        return message; 
    }
    
}

 

 

Just as start, this gets 15%

 

@isTest(SeeAllData=true)
public with sharing class PersonCoverageTest2 {
    static testMethod void sampleMethodTest() {
        
        
        List <Contact> contacts = new List<Contact>();
        String status = '';   
      	List<Global_Coverage__c> existingCoverageList = new List<Global_Coverage__c>();
      	List<User> users = new List<User>();
        Account target = new Account (name='target', Aims_client_id__c ='99999');
        insert target;
        
        Contact coveringuy = new Contact (lastname='bob', accountid=target.id , EMP_ID__c = 4683);
      	
      	List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
      	
        Coverage_Person__c coveragePerson = new Coverage_Person__c(emp_id__c = 4683, aims_client_id__c = '99999', aims_coverage_id__c=1,shortname__c = 'FIDEL', account__c=target.id  );
        
        List<Account> accounts = [SELECT Id FROM Account A where a.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
        
        List <Coverage_Person__c> coverageList = new List <Coverage_Person__c>();
        
	PersonCoverage.addCoveragePerson(coverageList);
        
        
        insert coveringuy;
        insert coverageperson;
        
        contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1 ];   
        
        System.assertNotEquals(accounts.size(),0);
        System.assertNotEquals(contacts.size(),0);
        
        Savepoint sv = Database.setSavepoint();
 	  
 	   accounts = [SELECT A.ID,A.NAME FROM Account A WHERE A.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
 	   contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1];
 	   users = [SELECT c.EMP_ID__c,c.Name FROM User c where c.EMP_ID__c = :coveragePerson.emp_id__c and IsActive=true limit 1];
 	   coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c and c.emp_id__c = :coveragePerson.emp_id__c limit 1 ];
        
        integer acctsize = accounts.size();
      	integer ctctssize = contacts.size();
        integer coveragesSz = coverages.size();
        
        
        system.assertNotequals(coveragesSz,0);
        system.assertNotequals(acctsize,0);
        system.assertNotequals(ctctssize,0);
        
       
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

Hi good

 here Coverages.size()==0 then only it will execute here Coverages.size() is not equal to zero that's why these lines are not executing .

  if(coverages.size() == 0 ){
95 status = 'Error';
96 if(message == '')
97 message = 'No Coverage for deleteCoveragePerson';
98 message += coveragePerson;
99 missingCoverages.add(coveragePerson);

 

You can do it.

All Answers

jd123jd123

Hi

 

Can you highlight which lines are not covered.

jaw999jaw999

I will say which lines are covered since it is a lot less than the ones that are not

Lines 1-13 are covered

 

 Webservice static String addCoveragePerson(Coverage_Person__c[] coverageList){
        
        List <Contact> contacts = new List<Contact>();
        List<Account> accounts = new List<Account>();
         List<User> users = new List<User>();
        List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
        String status = '';
        String message = '';
        List<Coverage_Person__c> missingCoverage = new List<Coverage_Person__c>();
        
        for(Coverage_Person__c coveragePerson:coverageList){

 also lines 73 and 77.

thanks

jd123jd123

Try with this below code

I

 

when you are calling your webservice method , you are sending it a blank list.frist you insert the Coverage Person and this one add to do Coveage list after that you can call the method. 

 

@isTest(SeeAllData=true)
public with sharing class PersonCoverageTest2 {
    static testMethod void sampleMethodTest() {
        
        
        List <Contact> contacts = new List<Contact>();
        String status = '';   
      	List<Global_Coverage__c> existingCoverageList = new List<Global_Coverage__c>();
      	List<User> users = new List<User>();
        Account target = new Account (name='target', Aims_client_id__c ='99999');
        insert target;
        
        Contact coveringuy = new Contact (lastname='bob', accountid=target.id , EMP_ID__c = 4683);
      	
      	List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
      	
        Coverage_Person__c coveragePerson = new Coverage_Person__c(emp_id__c = 4683, aims_client_id__c = '99999', aims_coverage_id__c=1,shortname__c = 'FIDEL', account__c=target.id  );
        
        List<Account> accounts = [SELECT Id FROM Account A where a.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
        
        List <Coverage_Person__c> coverageList = new List <Coverage_Person__c>();
        insert coveringuy;
        insert coverageperson;
coverageList.add(coverageperson); PersonCoverage.addCoveragePerson(coverageList); contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1 ]; System.assertNotEquals(accounts.size(),0); System.assertNotEquals(contacts.size(),0); Savepoint sv = Database.setSavepoint(); accounts = [SELECT A.ID,A.NAME FROM Account A WHERE A.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ]; contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1]; users = [SELECT c.EMP_ID__c,c.Name FROM User c where c.EMP_ID__c = :coveragePerson.emp_id__c and IsActive=true limit 1]; coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c and c.emp_id__c = :coveragePerson.emp_id__c limit 1 ]; integer acctsize = accounts.size(); integer ctctssize = contacts.size(); integer coveragesSz = coverages.size(); system.assertNotequals(coveragesSz,0); system.assertNotequals(acctsize,0); system.assertNotequals(ctctssize,0); } }
vishal@forcevishal@force

In your test method,

 

when you are calling your webservice method from the test code, you are sending it a blank list

 

ist <Coverage_Person__c> coverageList = new List <Coverage_Person__c>();
        
	PersonCoverage.addCoveragePerson(coverageList);

 Since the list is blank,

it doesn't go beyond this

 

for(Coverage_Person__c coveragePerson:coverageList){

 Because there are no records to iterate.

jaw999jaw999

Ah, ok, thanks. We have 47% i will show what is covered below. Sorry can't format code pasted in code style

 

1 Global class PersonCoverage {
2
3 Webservice static String addCoveragePerson(Coverage_Person__c[] coverageList){
4
5 List <Contact> contacts = new List<Contact>();
6 List<Account> accounts = new List<Account>();
7 List<User> users = new List<User>();
8 List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
9 String status = '';
10 String message = '';
11 List<Coverage_Person__c> missingCoverage = new List<Coverage_Person__c>();
12
13 for(Coverage_Person__c coveragePerson:coverageList){
14
15 Savepoint sv = Database.setSavepoint();
16
17 accounts = [SELECT A.ID,A.NAME FROM Account A WHERE A.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
18 contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1];
19 users = [SELECT c.EMP_ID__c,c.Name FROM User c where c.EMP_ID__c = :coveragePerson.emp_id__c and IsActive=true limit 1];
20 coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c and c.emp_id__c = :coveragePerson.emp_id__c limit 1 ];
21
22 if(accounts.size() == 0 ){
23 if(message == '')
24 message = 'No Inst for Account addPersonCoverage';
25 message += coveragePerson;
26 missingCoverage.add(coveragePerson);
27 status = 'Error';
28 //Notifier.DoNotification(' No Inst for Account addPersonCoverage::'+coveragePerson.aims_client_id__c,coveragePerson);
29 //return 'ERROR: Missing client: '+aimsClientId ;
30 continue;
31 }
32
33 if(contacts.size() == 0){
34 if(message == '')
35 message = 'No Contact for Account addPersonCoverage';
36 message += coveragePerson;
37 missingCoverage.add(coveragePerson);
38 //return 'ERROR: Missing client: '+aimsClientId ;
39 continue;
40 }
41
42 //if the coverage exists, then delete it first
43 if(coverages.size() != 0){
44 Coverage_Person__c deleteCoveragePerson = coverages[0];
45 try{
46 Database.delete(deleteCoveragePerson);
47 }catch(DmlException e){
48 Database.rollback(sv);
49 Notifier.DoNotification('PersonCoverage.deleteCoveragePerson: ERROR on deleteCoveragePerson: '+e.getDmlMessage(0), deleteCoveragePerson);
50 }
51 }
52
53 Account acct = accounts.get(0);
54 Contact contact = contacts.get(0);
55 if(users.size()!=0){
56 User user = users.get(0);
57 coveragePerson.User__c = user.Id;
58 }
59
60 coveragePerson.Account__c= acct.Id;
61 coveragePerson.Contact__c = contact.Id;
62 coveragePerson.name = contact.name;
63 try{
64 Database.insert(coveragePerson);
65 }catch(DmlException e){
66 Database.rollback(sv);
67 Notifier.DoNotification('PersonCoverage.addCoveragePerson: ERROR on Inserting Of Coverage: '+e.getDmlMessage(0), coveragePerson);
68 }
69
70
71 }
72
73 if(status == 'Error'){
74 Notifier.DoCoverageNotification(' Missing data on addCoveragePerson:::',missingCoverage);
75 }
76
77 return message;
78 }
79
80 Webservice static String deleteCoveragePerson(Coverage_Person__c[] coverageList){
81
82 String message = '';
83 String status = '';
84 List<Coverage_Person__c> missingCoverages = new List<Coverage_Person__c>();
85
86 List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
87
88 for(Coverage_Person__c coveragePerson:coverageList){
89
90 Savepoint sv = Database.setSavepoint();
91
92 coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c limit 1 ];
93
94 if(coverages.size() == 0 ){
95 status = 'Error';
96 if(message == '')
97 message = 'No Coverage for deleteCoveragePerson';
98 message += coveragePerson;
99 missingCoverages.add(coveragePerson);
100 //Notifier.DoNotification(' No Coverage for deleteCoveragePerson::'+coveragePerson.aims_coverage_id__c,coveragePerson);
101 //return 'ERROR: Missing client: '+aimsClientId ;
102 }else{
103 Coverage_Person__c deleteCoveragePerson = coverages[0];
104 try{
105 Database.delete(deleteCoveragePerson);
106 }catch(DmlException e){
107 Database.rollback(sv);
108 Notifier.DoNotification('PersonCoverage.deleteCoveragePerson: ERROR on Deleting Of Coverage: '+e.getDmlMessage(0), deleteCoveragePerson);
109 }
110 }
111
112 }
113
114 if(status == 'Error'){
115 Notifier.DoCoverageNotification(' No Coverage for deleteCoveragePerson::',missingCoverages);
116 }
117
118 return message;
119 }
120
121 }

jaw999jaw999

OK, just adding 

  PersonCoverage.deleteCoveragePerson(coverageList);

at the end gets me another 20%. I see now.

Am still having trouble with some lines:

 

 23   if(message == '')
 24   message = 'No Inst for Account addPersonCoverage';
 25   message += coveragePerson;
 26   missingCoverage.add(coveragePerson);
 27   status = 'Error';
 28   //Notifier.DoNotification(' No Inst for Account addPersonCoverage::'+coveragePerson.aims_client_id__c,coveragePerson);
 29   //return 'ERROR: Missing client: '+aimsClientId ;
 30   continue;

 

this shows up also at line 95.

and

 

47   }catch(DmlException e){
 48   Database.rollback(sv);
 49   Notifier.DoNotification('PersonCoverage.deleteCoveragePerson: ERROR on deleteCoveragePerson: '+e.getDmlMessage(0), deleteCoveragePerson);

 

which repeats at 106

 

thanks again

jd123jd123

Hi good

 here Coverages.size()==0 then only it will execute here Coverages.size() is not equal to zero that's why these lines are not executing .

  if(coverages.size() == 0 ){
95 status = 'Error';
96 if(message == '')
97 message = 'No Coverage for deleteCoveragePerson';
98 message += coveragePerson;
99 missingCoverages.add(coveragePerson);

 

You can do it.

This was selected as the best answer
jaw999jaw999

OK, i am a little confused. 

If the coverages.size is 0 than the messages below should fire, no?

so if i add an empty coveragelist that would work, but it is not. thanks

 

@isTest(SeeAllData=true)
public with sharing class PersonCoverageTest6 {
    static testMethod void sampleMethodTest() {
        
        
        List <Contact> contacts = new List<Contact>();
        String status = '';   
        List<Global_Coverage__c> existingCoverageList = new List<Global_Coverage__c>();
        List<User> users = new List<User>();
        Account target = new Account (name='target', Aims_client_id__c ='99999');
        insert target;
        
        Contact coveringuy = new Contact (lastname='bob', accountid=target.id , EMP_ID__c = 4683);
        
        List<Coverage_Person__c> coverages = new List<Coverage_Person__c>();
        
        Coverage_Person__c coveragePerson = new Coverage_Person__c(emp_id__c = 4683, aims_client_id__c = '99999', aims_coverage_id__c=1,shortname__c = 'FIDEL', account__c=target.id  );
        
        List<Account> accounts = [SELECT Id FROM Account A where a.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
        
        List <Coverage_Person__c> coverageList = new List <Coverage_Person__c>();
         List <Coverage_Person__c> coverageList2 = new List <Coverage_Person__c>();
        
        insert coveringuy;
        insert coverageperson;
        coverageList.add(coverageperson);
        PersonCoverage.addCoveragePerson(coverageList);
        contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1 ];   
        
        System.assertNotEquals(accounts.size(),0);
        System.assertNotEquals(contacts.size(),0);
        
        Savepoint sv = Database.setSavepoint();
      
       accounts = [SELECT A.ID,A.NAME FROM Account A WHERE A.AIMS_CLIENT_ID__c = :coveragePerson.aims_client_id__c limit 1 ];
       contacts = [SELECT c.EMP_ID__c,c.Name FROM Contact c where c.EMP_ID__c = :coveragePerson.emp_id__c limit 1];
       users = [SELECT c.EMP_ID__c,c.Name FROM User c where c.EMP_ID__c = :coveragePerson.emp_id__c and IsActive=true limit 1];
       coverages = [SELECT C.ID,C.NAME FROM Coverage_Person__c C WHERE C.aims_coverage_id__c = :coveragePerson.aims_coverage_id__c and c.emp_id__c = :coveragePerson.emp_id__c limit 1 ];
        
        integer acctsize = accounts.size();
        integer ctctssize = contacts.size();
        integer coveragesSz = coverages.size();
        
        
        system.assertNotequals(coveragesSz,0);
        system.assertNotequals(acctsize,0);
        system.assertNotequals(ctctssize,0);
        
        PersonCoverage.deleteCoveragePerson(coverageList);
       PersonCoverage.addCoveragePerson(coverages);
       PersonCoverage.deleteCoveragePerson(coverageList2);
    }
}

 

jaw999jaw999

ah, i see. 84% now. thanks!

jd123jd123

Great.

 

Still if you need any help please let me know.

 

If your question is resolved please mark it as accept the solution.