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 

Apex Testing REQUIRED_FIELD_MISSING

When I go to set my Apex class to the live production and validate it this error shows :- The error
Here is my Apex Code which is used as a controller (Underlined is in red after testing) :-
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
    }
    public void saveObjects(){
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}
Here is the Apex Testing code:-
@isTest

private class ApexTesting {

    static testMethod void myUnitTest() {
        myController myC = new myController();
        myC.saveObjects();
        
        // check that an Apprentice__c was created
        List<Apprentice__c> ApprenticeObjs = [SELECT Id FROM Apprentice__c];
        System.assert(ApprenticeObjs.size() > 0);

    }
}
Whats required to allow me to upload to the live production. Is it somthing im missing in my test class? and how do I implement it?

Thanks
 
Best Answer chosen by Daniel Bleasdale
Yogeshwar TailorYogeshwar Tailor
Hi Daniel,

Programme_Area__c is the required field on object Apprentice__c . so while inserting you have to give some value.
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
    }
    public void saveObjects(){
         ApprenticeObj.Programme_Area__c = 'Give any value because it is required field';
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}
Thanks,
Yogesh
 

All Answers

Yogeshwar TailorYogeshwar Tailor
Hi Daniel,

Programme_Area__c is the required field on object Apprentice__c . so while inserting you have to give some value.
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
    }
    public void saveObjects(){
         ApprenticeObj.Programme_Area__c = 'Give any value because it is required field';
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}
Thanks,
Yogesh
 
This was selected as the best answer
Daniel BleasdaleDaniel Bleasdale
Cheers Yogesh,
Ive applied this to my controller but the coverage is now 70 pecent.
Do you know what I can add to my Apex Testing to increase my coverage to reach the 75% requirement thankyou? 
Daniel BleasdaleDaniel Bleasdale
User-added image
Yogeshwar TailorYogeshwar Tailor
Hi Daniel,

Cool..!!Try this code..
 
@isTest

private class ApexTesting {

    static testMethod void myUnitTest() {

		Apprentice__c ap = new Apprentice__c();
		ap.Programme_Area__c = 'test';
		insert ap;
		
		Apprenticeship_Component__c ac = new Apprenticeship_Component__c();
		ac.Apprentice__c = ap.id;
		insert ac;
		
        myController myC = new myController();
        myC.saveObjects();
        
        // check that an Apprentice__c was created
        List<Apprentice__c> ApprenticeObjs = [SELECT Id FROM Apprentice__c];
        System.assert(ApprenticeObjs.size() > 0);

    }
}


Just check and let me know if you got any problem.

Thanks,
Yogesh
 
Daniel BleasdaleDaniel Bleasdale
Thanks again Yogesh unfortunatly that isnt working too. Seems along the right lines
User-added image
If I put the Code at the bottom I get the same results.

User-added image
Yogeshwar TailorYogeshwar Tailor
Hi Daniel,

Before calling the method in test class you have to insert two dummy record of Apprentice__c and Apprenticeship_Component__c

Copy this code as it is....In your code you put these lines after calling method.
@isTest

private class ApexTesting {

    static testMethod void myUnitTest() {

		Apprentice__c ap = new Apprentice__c();
		ap.Programme_Area__c = 'test';
		insert ap;
		
		Apprenticeship_Component__c ac = new Apprenticeship_Component__c();
		ac.Apprentice__c = ap.id;
		insert ac;
		
        myController myC = new myController();
        myC.saveObjects();
        
        // check that an Apprentice__c was created
        List<Apprentice__c> ApprenticeObjs = [SELECT Id FROM Apprentice__c];
        System.assert(ApprenticeObjs.size() > 0);

    }
}


User-added image

Please let me know if still you got any problem.

Thanks,
Yogesh

 
Daniel BleasdaleDaniel Bleasdale
Thankyou I'll give it a try tomorrow.
Daniel BleasdaleDaniel Bleasdale
I must be doing somthing wrong I get loads of errors and cant seem to get this working. Ive been doing it for god knows how long. I just cant map it or get my head around it. Its just problem after problem. I must be stupid not to get this.User-added image
Is this bit in the wrong place, no matter where I put it more errors apear -
User-added image
 
Yogeshwar TailorYogeshwar Tailor
Hi Daniel,

The Code of test class you mention in first image is correct only. 

But you have to remove that <b> from your code. after that you will not get any error.

Line 7 and Line 13 remove that <b> and </b>

Thanks,
Yogesh