function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SergiuSergiu 

writing test methods

I can't for the life of me grasp the concept of test methods. 

 

I have a very basic class that deactivates specific users but i don't know how to write a test method for it.

 

 

global class deactivateAccount {

webservice static String deactivateUserAccount(){

String myResult; // define the result that will be returned

Integer countUsers = 0; // count the updated users

List<User> myUsers = [

SELECT u.Profile.Name, u.isActive

FROM User u

WHERE u.Profile.Name = 'test' and u.IsActive = TRUE

];

for (User aUser: myUsers){

aUser.IsActive = FALSE;

countUsers++;

}

update myUsers;

return 'done: ' + countUsers;

}

}

 

 How do I test this? Can someone EXPLAIN what i'd be testing.

 

Thank you!

 

 

 

Message Edited by Sergiu on 03-04-2010 10:32 PM
Best Answer chosen by Admin (Salesforce Developers) 
NZ_XueNZ_Xue

it is my idea here, but i did not run it and i like to konw the result from your thanks:

 

public static testMethod void testdeactivateUserAccount()

    {
      deactivateUserAccount();
      Profile p = [select id from profile where name='test' limit 1];

    User testuser = new User(profileid = p.Id, IsActive = TRUE, alias = 'test', email='test@hotmail.com',
             lastname='Testing', country='New Zealand', username='test@hotmail.com');
        insert testuser;
      String mes = deactivateUserAccount();
      System.assertEquals(FALSE,[select u.ISACTIVE from User u WHERE Id=:testuser.Id].IsACTIVE);
      SYstem.assertEquals('done: 1',mes);

     }

Message Edited by NZ_Xue on 03-04-2010 11:37 PM

All Answers

NZ_XueNZ_Xue

it is my idea here, but i did not run it and i like to konw the result from your thanks:

 

public static testMethod void testdeactivateUserAccount()

    {
      deactivateUserAccount();
      Profile p = [select id from profile where name='test' limit 1];

    User testuser = new User(profileid = p.Id, IsActive = TRUE, alias = 'test', email='test@hotmail.com',
             lastname='Testing', country='New Zealand', username='test@hotmail.com');
        insert testuser;
      String mes = deactivateUserAccount();
      System.assertEquals(FALSE,[select u.ISACTIVE from User u WHERE Id=:testuser.Id].IsACTIVE);
      SYstem.assertEquals('done: 1',mes);

     }

Message Edited by NZ_Xue on 03-04-2010 11:37 PM
This was selected as the best answer
SergiuSergiu
you rock! the coverage is 77%, can you explain the reasoning, the train of thoughts you had writing the test method? I'm trying to understand the concept.
NZ_XueNZ_Xue
i like to see those code that have not been tested, could you list them? thanks
SergiuSergiu

it all checked to 100% ... I had to change the profile name from test to the actual profile name I have in my org and then it jumped to 100% coverage. And I think I get the idea of testing as well:

1. prepare dummy data

2a. if you test a trigger then do the action that would trip the trigger.

2b. if you test a method/function then call that function

3. write system.assert  to write to the log file

 

4? check results returned by function/trigger

 

 

NZ_XueNZ_Xue

My mistake, I should remind you to get profile name from your sandbox.  I doubted about the 77%coverage,

 

 :smileyvery-happy:  100%  is acceptable one. few suggestions :

 

1 100% is not necessary.

2 you do not have to hard code the profile name if your konw how to use SystemRunAs(user) .(teststart(), teststop()...)

3 focus on user cases not coding lines coverage.

 

 

CalebECalebE

Hello,

 

Could you make a suggestion on how to test the following:

 

 

global class MassLogNoAnswer {
  WebService static Integer insertTasks(Id[] iParentIds) {
    Task[] tasks = new Task[0];
    for (Id iParentId : iParentIds) {
        Tasks.add(new Task(WhoId = iParentId,
                           Type = 'No Answer',
                           Status = 'Completed'));
    }
    insert tasks; //Bulk Insert 
     
    return tasks.size();
  }
}

 

miteshsuramiteshsura

Dear NZ_Xue,

 

I have very similar class structure and web service, but instead of SOQL, it has a SOSL query. 

This web service is called from a custom button on VF page, everyhting works good, but when I call the web service from a test class, SOSL retuns nothing .. any reason as to why this may be happnening? 

global class ProductAdminWebService { 
    
    //This method is called from custom 'Go Live' button on Product Administration page
    webservice static string goLive(string recordId, string status, string productRegion){   
        
        Product_Administration__c pa;
        //Stores Products and its related Services returned by SOSL
        List<List<SObject>> searchList;
        
        system.debug('recordId: ' + recordId);
        system.debug('status: ' + status);
        system.debug('productRegion: ' + productRegion);
        
        //Display an error message if record status is not approved or scheduled
        if(status != 'Approved' && status != 'Scheduled'){
            return 'The updates needs to be approved before it can go live. Please contact your system administrator for further details.';
        }
        else{
        	            
            //1. Run SOQL to get List<List<SObject>> of Product_Administration__c and all its related Service_Administration__c
            //searchList[0]: list<Product_Administration__c>
            //searchList[1]: list<Service_Administration__c>
            
            if(productRegion == 'NA'){
	            searchList = [FIND :productRegion IN ALL FIELDS
	                                              RETURNING 
	                                                 Product_Administration__c (id, Description__c, Product__c, Product_Region__c, AP_Price__c, Direct_Price__c, Dist_Price__c, LA_Price__c, Standard_Price__c, Schedule_Date__c WHERE Id= :recordId), 
	                                                 Service_Administration__c (id, Description__c, Product__c, Product_Region__c, AP_Price__c, Direct_Price__c, Dist_Price__c, LA_Price__c, Standard_Price__c WHERE Product_Administration__c= :recordId LIMIT 7)
	                                              LIMIT 8];
            }
            else if(productRegion == 'EMEA'){
	            searchList = [FIND :productRegion IN ALL FIELDS 
	                                              RETURNING 
	                                                 Product_Administration__c (id, Description__c, Product__c, Product_Region__c, DISTID__c, DISTIE__c, SID__c, SIE__c, VADD__c, VADE__c, Standard_Price__c, Schedule_Date__c WHERE Id= :recordId), 
	                                                 Service_Administration__c (id, Description__c, Product__c, Product_Region__c, DISTID__c, DISTIE__c, SID__c, SIE__c, VADD__c, VADE__c, Standard_Price__c WHERE Product_Administration__c= :recordId LIMIT 9)
	                                              LIMIT 10];

.....
.....
.....
.....
     }

   }
}