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
Admin DNAAdmin DNA 

test class I can't get 75% coverage but only 33%

This is the class:
public without sharing class calculatorController {
@AuraEnabled
public static void saveOpp (Id oppId, Decimal netto, Decimal capfin, Decimal comm, Decimal durata, Decimal interessi, Decimal rata, Decimal tan, String finalita, Id istituto ) {
try {
 
    Opportunity queriedOpp = [SELECT Id, Prev_Netto_Erogato__c, Amount 
                              FROM Opportunity 
                              WHERE Id = :oppId
                              LIMIT 1];

    
    queriedOpp.Prev_Netto_Erogato__c = netto;
    queriedOpp.Prev_Capitale_Finanziato__c = capfin;
    queriedOpp.Prev_Commissioni__c = comm;
    queriedOpp.Prev_Durata__c = durata;
    queriedOpp.Prev_Interessi__c = interessi;
    queriedOpp.Prev_Rata__c = rata;
    queriedOpp.Tan__c = tan;
    queriedOpp.Prev_Finalita__c = finalita;
    queriedOpp.Prev_Istituto_Finanziario__c = istituto;
    

    
    update queriedOpp;
} catch(Exception e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}

and this is the test
@isTest 
private class calculatorControllerTEST { 
    private static testMethod void saveOppTEST () { 
		date d1 = date.newInstance(2016,01,12);
        Opportunity opp = new Opportunity(Name='aaa', CloseDate=d1, StageName='Qualificata');
        insert opp;

        calculatorController ctrl = new calculatorController ();
        calculatorController.saveOpp('0061j00000235a7', 333, 333, 333, 333, 333, 333, 3, 'CQS', '0011j000002GwXp');
    
    } 
}

​I can get only 33% of coverage.
Any Ideas?
Thank you in advance
Raj VakatiRaj Vakati
try this code
@isTest 
private class calculatorControllerTEST { 
    private static testMethod void saveOppTEST () { 
		date d1 = date.newInstance(2016,01,12);
        Opportunity opp = new Opportunity(Name='aaa', CloseDate=d1, StageName='Qualificata');
        insert opp;

		// Add other fields - -This id you have to pass it to last agr
		Prev_Istituto_Finanziario__c  ins = new Prev_Istituto_Finanziario__c () ;
		ins.Name ='Demo' ;
		insert ins ; 
		
        calculatorController ctrl = new calculatorController ();
        calculatorController.saveOpp(opp.Id, 333, 333, 333, 333, 333, 333, 3, 'CQS', ins.Id);
    
    } 
	private static testMethod void saveOppTESTException() { 
		date d1 = date.newInstance(2016,01,12);
        Opportunity opp = new Opportunity(Name='aaa', CloseDate=d1, StageName='Qualificata');
        insert opp;
try{
        calculatorController ctrl = new calculatorController ();
        calculatorController.saveOpp(null, 333, 333, 333, 333, 333, 333, 3, 'CQS', null);
}catch(Exception e){
}
    
    } 
	
}

 
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Issue was coming due to hard coded ID in your Test class. Please create test data and provide same in Test class

Update your code like below
@isTest 
private class calculatorControllerTEST { 
    private static testMethod void saveOppTEST () { 
	
		Account acc = new Account();
		acc.Name ='Test'l
		insert acc;
		
		date d1 = date.newInstance(2016,01,12);
        Opportunity opp = new Opportunity(Name='aaa', CloseDate=d1, StageName='Qualificata' ,accountid = acc.id);
        insert opp;

        calculatorController ctrl = new calculatorController ();
        calculatorController.saveOpp(opp.Id, 333, 333, 333, 333, 333, 333, 3, 'CQS', acc.id);
    
    } 
}

Let us know if this will help you