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
Siva AdminSiva Admin 

Coverage for Test class. Kindly help

Hi all, I'm a newbee to SF. working on creating a test class below. Not getting the coverage at help. You help will be much appreciated. Thanks/

@IsTest
public class testclassownerassignmanager{
 static testmethod void metest(){
    Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
User testUserA = new User(
    Alias = 'standard', Email='standarduser@testorg.com',  
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = pro.Id, 
    TimeZoneSidKey='America/Los_Angeles', 

UserName='testUserA@testorganise.com'); 

User testUserB = new User(
    Alias = 'standard', Email='standarduser@testorg.com',  
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = pro.Id,  
    TimeZoneSidKey='America/Los_Angeles', 

UserName='testUserB@testorganise.com'); 
testUserB.ManagerID = testUserA.id;

List<User> usersToInsert = new List<User>();
usersToInsert.add(testUserA);
usersToInsert.add(testUserB);
insert usersToInsert;
  
  List<Account> acclist= new List<Account>();
  for(Integer i=0;i<=10;i++){
  account a = new account(Name='Test' + i, ownerid=testUserB.id);
    acclist.add(a);
  
  List<Account> TobUpdateAcclist= new List<Account>();
  for(Account ac: acclist){
     if (ac.owner.isactive == false )
      {
       ac.ownerid=testUserB.managerid;
       TobUpdateAcclist.add(a);
      }
      }
  
  test.starttest();
  if (Test.isRunningTest()) {
      System.runAs(new User(Id = Userinfo.getUserId())) {
       update TobUpdateAcclist;
      }
    } else {
       update TobUpdateAcclist;
    }
  test.stoptest();
  List<Account> updatedaccs = [select id,name from Account where id IN:TobUpdateAcclist];
   system.assertEquals(a.owner.Managerid, a.ownerid);
   }}}
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] [5] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
[5] https://developer.salesforce.com/trailhead/module/apex_testing