+ Start a Discussion
junkiyjunkiy 

i need to cover the test class in below code plz help any one new to salesforce

public class junctionRelated {
    private String casid {get; set;}
    public List<Junction_Data__c> vsts{get;set;} 
    public String Name {set;get;}
    public VSTS__C v{get;set;}
    public List<wVSTS> wraplist {get; set;}
    public list<VSTS__c> selected_vsts {get; set;}
    public Boolean check_box {get;set;}
    
    public junctionRelated(ApexPages.StandardController controller){
        casid = ApexPages.CurrentPage().getparameters().get('id');  
        v= new VSTS__C();
        vsts=[Select Id,VSTS__C,Case__c,VSTS__r.Name, VSTS__r.VSTS_Summary__c,VSTS__r.VSTS_Issue_Type__c From Junction_Data__c
              where Case__c=:casid order by LastModifiedDate desc];
    }
    public pagereference  save()
    {
        insert v;
        Junction_Data__c JVS = new Junction_Data__c (Case__c=casid ,VSTS__c=v.id);
        insert JVS;
        PageReference redirectPage = new PageReference('/'+casid);
        redirectPage.setRedirect(false);
        return redirectPage;  
    }
    public PageReference Cancel() {
        PageReference redirectPage = new PageReference('/'+casid); 
        redirectPage.setRedirect(false);
        return redirectPage;
        
    }
    
    public pageReference editVSTS()
    {  
        String VSTSId = Apexpages.currentpage().getParameters().get('VSTSId');  
        pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + VSTSId + '/e?retURL=' + casid);  
        return pageRef;   
    } 
    public pageReference newVSTS(){   
        pageReference pageRef = new pageReference('/apex/JunctionRelatedPAge?id='+casid);
        return pageRef; 
    } 
    
    public pageReference deleteVSTS()
    {  
       // String VSTSId = Apexpages.currentpage().getParameters().get('VSTSId');  
        VSTS__C  vsts = [SELECT Id FROM VSTS__c WHERE id =:v.Id LIMIT 1]; 
        delete vsts; 
        PageReference redirectPage = new PageReference('/'+casid);
        return redirectPage;
        
    }
    public pagereference gotolink(){
        pageReference pageRef = new pageReference('/apex/Gotolist');
        return pageRef;
    }
    public PageReference Back() {
        PageReference redirectPage = new PageReference('/'+casid); 
        redirectPage.setRedirect(false);
        return redirectPage;
        
    }
    public PageReference Linktocase()
    {
        selected_vsts=new list<VSTS__c>();
        for(wVSTS cc: wraplist)
        {
            if(cc.check_box)
            {
                selected_vsts.add(cc.V);
            }
        }
        list<Junction_Data__c> validate =[Select Case__c,VSTS__c From Junction_Data__c where VSTS__c IN (Select Id From VSTS__C where Id=:selected_vsts)];
        set<Id> added_list = new set<Id>();
        if(!validate.isEmpty())
        {
            for(Junction_Data__c add_vst : validate)
            {
                if(add_vst.Case__c != casid)
                    added_list.add(add_vst.VSTS__c);
            }
        }
        //linking with case by creating a junction record 
        list<Junction_Data__c> junction = new  list<Junction_Data__c>();
        for(VSTS__c vs : [Select Id From VSTS__c Where Id =:added_list])
        {
            Junction_Data__c junc = new Junction_Data__c();
            junc.Case__c=casid;
            junc.VSTS__c=vs.Id;
            junction.add(junc);
        }
        insert junction;
        PageReference redirectPage = new PageReference('/'+casid); 
        return redirectPage;
    }
    
     //Wrapper class
    public class wVSTS
    {
        public VSTS__c V{set;get;}
        public Boolean check_box {get; set;}   
        //constructor
        public wVSTS(VSTS__c V, Boolean check_box) 
        {
            this.V = V;
            this.check_box = check_box;
        }
    }
    }
vijay kumar kvijay kumar k
Hi EST Expert

For every test classes we just look in to what are queries are we used and how if conditions we should check and what all inputs are required that's it.
 
@IsTest 

Public class junctionrelated_Test(){
@istest Public static void junctionrelatedtestmethod(){
// First insert test data on junciondata object with satiesfied where condition for you required //case id so first we should create case record
Case c= new Case ();
C.Status = 'Open';
Insert c;

VSTS__c vs = New VSTS__c(VSTS_Summary__c = '%%%',Name ='!!!',VSTS_Issue_Type__c = 'Type1');
insert vs;
Junction_Data__c jd = new Junction_Data__c();
jd.Name ='Test Data';
jd.VSTS__c = vs.id;
jd.Case__c =c.id;
Insert jd;

// you try to insert VSTS__C records in save method means your getting all inputs of this object from page.So give test data here.
VSTS__C vs1 = New VSTS__C();
vs1.Name = 'Test VSTS';
// do not give insert operatioin;

// Declare Your Apex Class Here and call each and every method.
junctionRelated JR = New junctionRelated();
// Pass every input from here;
JR.casid = c.Id;
Jr.v = vs1;// using this value record gets created in save method;
Jr.save();
Apexpages.currentpage().setParameters('VSTSId',vs.id);
Jr.editVSTS();
jr.newVSTS();
jr.deleteVSTS();
jr.gotolink();
jr.Back();
jr.Linktocase();
}
}

I this will be cover most of the class.
Note: You need to give all required data while doing insert operation to avoid validation errors.

Please MARK this if it's useful to you; 

Regards 
Vijay
junkiyjunkiy
hii vijay,
Thanks for your replaying but its not cover the wrapper class again its show same error not the code coverage .