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
Daniel BleasdaleDaniel Bleasdale 

Increase my coverage above 70%

Basically Im getting problem after problem, I had to make an Apex class to use as a coustom contoller which all worked well and then I proceeded to "try" and put it on live where I was stopped dead in my tracks because of code coverage after struggling for ages then finally getting an answer on the form I was able to achive 77% coverage. I went to upload to live again and then I got the error :-The error
After applying a work around my code coverage has dropped to 70% and im really struggling to get the coverage back upto the 75% threshold to even begin testing if the work around does the job. The testing still compleatly confuses me and Im an absolute begginer so I dont know how to format or develop in Apex too well as of yet. Was just hoping if somone can point me in the right direction for gaining the 75% required.

This is my 70% that is covered:-
User-added image  
Here is my Testing Class:-
 User-added image

Thankyou,
Sorry if its super simple and I cant process it.
Best Answer chosen by Daniel Bleasdale
Steven NsubugaSteven Nsubuga
Because ProgramArea is a Master of Apprentice__c. I have added it in the Controller.

I see that ProgramArea has a look up to Curriculum Area. I have ignored it in this case. But you can add it if you must, similar to my addition of ProgramArea. Fornow, to get things moving, mark that look up relationship as not required. 
Your data model is quite complex, you might want to get a Developer on site if things get more hairy. 

Updated Controller​
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public Programme_Area__c ProgramArea {get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
         ProgramArea = new Programme_Area__c ();
    }
    public void saveObjects(){
         insert ProgramArea;
         ApprenticeObj.Programme_area__c = ProgramArea.Id;
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}

Updated Test
@isTest
public class myControllerTest {
    @isTest static public  void insertController(){
               
        myController my = new myController();
        my.saveObjects();

        List<Programme_Area__c> ProgramArea = [SELECT Id FROM Programme_Area__c];
        System.assert(ProgramArea.size() > 0);

        List<Apprentice__c> a = [SELECT Id FROM Apprentice__c]; 
        System.assert(a.size() > 0);

        List<Apprentceship__c> ac = [SELECT Id FROM Apprentceship__c];
        System.assert(ac.size() > 0);
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
The test is failing!
Check the Logs section of the Developer Console, the one just before the Tests section at the bottom of the screen.
It looks like the insert fails, check the Logs section to see why. I suspect that the Programme_Area__c field is not happy with the 'Test' value, causing the insert to fail. Is Programme_Area__c  a picklist field? If so, ensure that you give it a valid option in the test method.
PawanKumarPawanKumar
As per screenshot in red, I can tell you your insert is failing due to some other reason(maybe another mandatory field). so can you please expand the '+' sign and see why test method has failed. please post the error message for test method failure.


 
JuFeJuFe
Hi Daniel,

I've been checking your question and I think this is what your test method should look like:

@isTest
static void myUnitTest() {
    
    myController myC = new myController();
    
    // Here you will be setting the value for the required field in the 
    // record that will be saved by your method.
    myC.ApprenticeObj.Programme_Area__c = 'test';
    
    // It's a good practice to wrap your method in test 
    // by the startTest and stopTest methods
    Test.startTest();
        myC.saveObjects();
    Test.stopTest();
    
    // Finally, do your assertions as you were doing, which is a good practice.
    ...
}

Please have a look at this and let me know if it makes sense and solves your problem.

Good luck!
Julio
PawanKumarPawanKumar

to troubleshoot, you can run the below code in an Annonymous block and can see the insert failure.

Apprentice__c ap = new Apprentice__c();
ap.Programme_Area__c = 'test';
insert a;
Daniel BleasdaleDaniel Bleasdale
User-added imageUser-added imageUser-added image
sahana reddysahana reddy
Hi

User-added image
My Test Class:
---------------------------

@isTest
public class myControllerTest {
    @isTest static public  void insertController(){
        Apprentice__c a = new Apprentice__c();
        a.Programme_area__c = 'test';
        insert a;
        Apprentceship__c ac = new Apprentceship__c();
        ac.Apprentice__c = a.id;
        insert ac;
        myController my = new myController();
        my.saveObjects();
    }
}
Daniel BleasdaleDaniel Bleasdale
User-added imageIm honestly so lost I hate this testing thing m just not processing , What would a valid option be. Im in way too deep. Will it make a difference that ProgramArea is a master and Apprentice is a child. I have no idea how people are getting 100% cover and I get errors.

Im sorry if im being very stupid
 
Steven NsubugaSteven Nsubuga
Will it make a difference that ProgramArea is a master and Apprentice is a child?
Yes, huge difference!!
That means a ProgramArea record must exist, and its ID is meant to go in the Programme_Area__c field.
As it is, I expect that even your visualforce page and controller are no longer valid.

Share with us all the fields of the ProgramArea object and we'll help you update your code.
And no, you are not being stupid.
Daniel BleasdaleDaniel Bleasdale
I see thankyou, hopfully thats the answer to why nothing seems to work for me.
User-added imageUser-added imageUser-added imageUser-added image
Steven NsubugaSteven Nsubuga
Because ProgramArea is a Master of Apprentice__c. I have added it in the Controller.

I see that ProgramArea has a look up to Curriculum Area. I have ignored it in this case. But you can add it if you must, similar to my addition of ProgramArea. Fornow, to get things moving, mark that look up relationship as not required. 
Your data model is quite complex, you might want to get a Developer on site if things get more hairy. 

Updated Controller​
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public Programme_Area__c ProgramArea {get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
         ProgramArea = new Programme_Area__c ();
    }
    public void saveObjects(){
         insert ProgramArea;
         ApprenticeObj.Programme_area__c = ProgramArea.Id;
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}

Updated Test
@isTest
public class myControllerTest {
    @isTest static public  void insertController(){
               
        myController my = new myController();
        my.saveObjects();

        List<Programme_Area__c> ProgramArea = [SELECT Id FROM Programme_Area__c];
        System.assert(ProgramArea.size() > 0);

        List<Apprentice__c> a = [SELECT Id FROM Apprentice__c]; 
        System.assert(a.size() > 0);

        List<Apprentceship__c> ac = [SELECT Id FROM Apprentceship__c];
        System.assert(ac.size() > 0);
    }
}

 
This was selected as the best answer
Daniel BleasdaleDaniel Bleasdale
Im sorry to keep bothering you, but I get these errors, how do I go about resolving them?
User-added image
User-added image
User-added image
Steven NsubugaSteven Nsubuga
That was my mistake
@isTest
public class myControllerTest {
    @isTest static public  void insertController(){
               
        myController my = new myController();
        my.saveObjects();

        List<Programme_Area__c> ProgramArea = [SELECT Id FROM Programme_Area__c];
        System.assert(ProgramArea.size() > 0);

        List<Apprentice__c> a = [SELECT Id FROM Apprentice__c]; 
        System.assert(a.size() > 0);

        List<Apprenticeship_Component__c > ac = [SELECT Id FROM Apprenticeship_Component__c ];
        System.assert(ac.size() > 0);
    }
}

 
Daniel BleasdaleDaniel Bleasdale
User-added imageThankyou, this is the closest I have been.
Steven NsubugaSteven Nsubuga
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public Programme_Area__c ProgramArea {get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
         ProgramArea = new Programme_Area__c ();
    }
    public void saveObjects(){
         ProgramArea.PA_Code__c = 'Test';
         insert ProgramArea;
         ApprenticeObj.Programme_area__c = ProgramArea.Id;
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}

 
Daniel BleasdaleDaniel Bleasdale
Thankyou, that gets me back to 71% cover
Daniel BleasdaleDaniel Bleasdale
User-added imageThankyou for helping too.
Daniel BleasdaleDaniel Bleasdale
Im upto 85% now, hopfully it lets me go to production
Daniel BleasdaleDaniel Bleasdale
User-added image
Hopfully this is my last question but this is whats stopping me from going live, what do I add? Thankyou
Steven NsubugaSteven Nsubuga
Hi Daniel, the trick is to figure out the principle. 
The error states that you are missing a required field in line 16 of the controller.
line 16 is the one that inserts ComponentsObj. 
The required field, according to the error message is Apprenticeship_Qualifications__c.

With that established, add the following line to the controller,
ComponentsObj.Apprenticeship_Qualifications__c = 'Test';
just above the insert ComponentsObj
FYI, I used 'Test' but if that field is a picklist then the actual value should be one of the existing options in the picklist.

The complete controller class becomes
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public Programme_Area__c ProgramArea {get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
         ProgramArea = new Programme_Area__c ();
    }
    public void saveObjects(){
         ProgramArea.PA_Code__c = 'Test';
         insert ProgramArea;
         ApprenticeObj.Programme_area__c = ProgramArea.Id;         
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         ComponentsObj.Apprenticeship_Qualifications__c = 'Test';
         insert ComponentsObj;
    }
}
Remember to use valid picklist options where applicable.