You need to sign in to do that
Don't have an account?
Indianajones
help me please with test class
I create apex class and i need test class for this code:
i new in salesforce and i create test class, but code coverage 0%. i study and i dont know how i can do 75+ code coverage.
thanks.
global with sharing class controllerTest { public Opportunity opportunity {get; set;} public List<OpportunityLineItem> oppLineItem {get; set;} public list<OpportunityContactRole> contact {get; set;} @AuraEnabled public static void savePDF(id recordId){ Opportunity opps = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId]; List<ContentVersion> contentVersionss = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: opps.Invoice_Number__c]; if(!contentVersionss.isEmpty()){ Id contentDocumentId = contentVersionss.get(0).ContentDocumentId; Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId]; PageReference pdfPage = new PageReference('/apex/newPageVf'); pdfPage.getParameters().put('Id', recordId); Blob pdfBlob = pdfPage.getContentAsPDF(); String fileName = opp.Invoice_Number__c; List<ContentVersion> contentVersions = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: fileName]; ContentVersion cv = new ContentVersion(); cv.Title = fileName; cv.PathOnClient = fileName + '.pdf'; cv.VersionData = pdfBlob; cv.contentDocumentId = contentDocumentId; INSERT cv; } else { Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId]; PageReference pdfPage = new PageReference('/apex/newPageVf'); pdfPage.getParameters().put('Id', recordId); Blob pdfBlob = pdfPage.getContentAsPDF(); String fileName = opp.Invoice_Number__c; ContentVersion cv = new ContentVersion(); cv.Title = fileName; cv.PathOnClient = fileName + '.pdf'; cv.VersionData = pdfBlob; INSERT cv; Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId; Id opportunId = [SELECT Id FROM Opportunity WHERE id =: recordId].Id; ContentDocumentLink cdl = new ContentDocumentLink(); cdl.ContentDocumentId = contentDocumentId; cdl.LinkedEntityId = opportunId; cdl.ShareType = 'V'; INSERT cdl; } } // Берём данные для VisualForce документа. public controllerTest(ApexPages.StandardController stdController){ Id oppId = apexpages.currentpage().getparameters().get('id'); this.opportunity = [SELECT Id, Name, OwnerId, AccountId, Amount, Invoice_Number__c FROM Opportunity WHERE Id =: oppId]; this.oppLineItem = [SELECT OpportunityId, Name, Quantity, UnitPrice, TotalPrice FROM OpportunityLineItem WHERE OpportunityId =: oppId]; this.contact = [SELECT id, ContactId, Contact.Name, Contact.Phone, Contact.Email, IsPrimary, Contact.Account.Name FROM OpportunityContactRole WHERE OpportunityId =: oppId AND IsPrimary = true]; } }
i new in salesforce and i create test class, but code coverage 0%. i study and i dont know how i can do 75+ code coverage.
@isTest public class controllerTestTest{ @isTest public Static Void UnitTest(){ Account Acc =new Account(); Acc.Name = 'Account test'; Insert Acc; Opportunity opp = new Opportunity(); opp.Name = 'test opp'; opp.Stagename = 'Closed Won'; opp.CloseDate = System.today(); opp.AccountId = Acc.Id; insert Opp; ContentVersion contentVersionInsert = new ContentVersion( Title = 'Test', PathOnClient = 'Test.jpg', VersionData = Blob.valueOf('Test Content Data'), IsMajorVersion = true ); insert contentVersionInsert; ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1]; List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; System.assertEquals(documents.size(), 1); } }help me please.
thanks.
Can you try the below code. I have made small changes in the Apex controller as some methods wont support in test class as highlited. It is covering around 79% of code.
Test Class:
If this solution helps, Please mark it as best answer.
Thanks,