+ Start a Discussion
Kiran PandiyanKiran Pandiyan 

How do I get 80% coverage for this class ?

This is my class
public class APTS_OrderRequest {
	public class Order {
        public String OrderSource{get;set;}
        public String PurchaseOrderNumber{get;set;}
        public String POOrderDate;
        public String SalesResponsibleUnit{get;set;}
        public String QuotationReference{get;set;}
        public String ApttusQuotationID{get;set;}
        public String QuotationRevisionNo{get;set;}
        public String QuoteCreationDate{get;set;}
        public String QuoteReferenceDescription{get;set;}
        public String EndUserCustomerIdentifier{get;set;}
        public String CustomerGUID{get;set;}
        public String PlacedBy{get;set;}
        public String ProjectName{get;set;}
        public String CurrencyCode{get;set;}
        public String SourceSystemOrderNumber{get;set;}
        public String OrderDate{get;set;}
        public String OrderingSystem{get;set;}
        public String HandledBy{get;set;}
        public String AdditionalSalesPersonEmail{get;set;}
        public String ExpectedDeliverDate{get;set;}
        public String IndustryUsage{get;set;}
        public String ChannelClass{get;set;}
        public string TenderLocationLevel2ID{get;set;}
        public String CustomerReference{get;set;}
        public String Application{get;set;}
        public String IndustryUsageLevel1{get;set;}
        public String IndustryUsageLevel2{get;set;}
        public Warranty Warranty{get;set;}
	}
	public class Warranty {
        public String WarrantyMax{get;set;}
        public String Warranty{get;set;}
        public String QualityControl{get;set;}
        public String Penalty{get;set;}
        public String Packing{get;set;}
        public string SpecialWarranty{get;set;}
    }

    public class ConfiguredItem {
        public String ConfigurationId{get;set;}
        public String NomenclatureNumber{get;set;}
        public String ConfiguratorId{get;set;}
        public String ConfiguratorPlatform{get;set;}
    }
	public class OrderLineItems {
        public String SourceSystemItemId{get;set;}
        public String ApttusQuotationLineID{get;set;}
        public Integer LineNumber{get;set;}
        public Integer ItemSequence{get;set;}
        public String ProductID{get;set;}
        public String ProductNumber{get;set;}
        public String ProductDescription{get;set;}
        public string PartID{get;set;}
        public string ProductType{get;set;}
        public string TechnicalID{get;set;}
        public string ClassificationID{get;set;}
        public string StandardCategoryType{get;set;}
        public string CategoryID{get;set;}
        public Integer Quantity{get;set;}
        public String CurrencyCode{get;set;}
        public String UnitOfMeasure{get;set;}
        public Decimal UnitPrice{get;set;}
        public String DeliveryDate{get;set;}
        public String ContractDeliveryDate{get;set;}
        public Integer ManualLeadTime{get;set;}
        public String ProductionUnit{get;set;}
        public Decimal ListPrice{get;set;}
        public Decimal TotalNetSalesPrice{get;set;}
        public Decimal NetSalesPrice{get;set;}
        public Decimal TransferPrice{get;set;}
        public String TransferPriceCurrency{get;set;}
        public Decimal StandardDiscount{get;set;}
        public Decimal DiscountPercentage{get;set;}
        public String ParentLineNumber{get;set;}
        public String RequiredLeadTime{get;set;}
        public Decimal BaseCost{get;set;}
        public Decimal AdditionalDiscount{get;set;}
        public String Supplier{get;set;}
        public String Customer{get;set;}
        public String EndUserCountry{get;set;}
        public String PurchaseOrderNumber{get;set;}
        public String PurchaseOrderLineNumber{get;set;}
        public String TransportMode{get;set;}
        public Boolean Send2ERP{get;set;}
        public String TopArticle{get;set;}
        public String Warehouse{get;set;}
        public Notes Notes{get;set;}
        public ConfiguredItem ConfiguredItem{get;set;}
        
	}
}

This is my test class. I was getting 20% coverage before but after adding warranty fields. I'm getting an error
@isTest
public class APTS_OrderRequestTest {

     public static testmethod void testMethod1() {
        APTS_OrderRequest objRequest = new APTS_OrderRequest();
        APTS_OrderRequest.Order objOrder = new APTS_OrderRequest.Order();
		objOrder.OrderSource= '';
		objOrder.PurchaseOrderNumber= '';
        objOrder.POOrderDate= date.today();
        objOrder.SalesResponsibleUnit= '';
        objOrder.QuotationReference= '';
        objOrder.ApttusQuotationID= '';
        objOrder.QuotationRevisionNo= '';
        objOrder.QuoteCreationDate= '';
        objOrder.QuoteReferenceDescription= '';
        objOrder.EndUserCustomerIdentifier= '';
        objOrder.CustomerGUID= '';
        objOrder.PlacedBy= '';
        objOrder.ProjectName= '';
        objOrder.CurrencyCode= '';
        objOrder.SourceSystemOrderNumber= '';
        objOrder.OrderDate= '';
        objOrder.OrderingSystem= '';
        objOrder.HandledBy= '';
        objOrder.AdditionalSalesPersonEmail= '';
        objOrder.ExpectedDeliverDate= '';
        objOrder.IndustryUsage= '';
        objOrder.ChannelClass= '';
        objOrder.TenderLocationLevel2ID= '';
        objOrder.CustomerReference= '';
        objOrder.Application= '';
        objOrder.IndustryUsageLevel1= '';
        objOrder.IndustryUsageLevel2= '';
		objOrder.Warranty= '';
		objOrder.ExternalChannelPartnerId= '';
		objOrder.PaymentNDeliveryTerms= '';
		objOrder.Links= '';
		objOrder.OrderLineItems= '';
		objOrder.Addresses= '';
		objOrder.MileStoneBillingPlan= '';
		objOrder.SalesPersonDetails= '';
		APTS_OrderRequest.Order.Warranty testWarranty = new APTS_OrderRequest.Order.Warranty();
		testWarranty.WarrantyMax= '';
		testWarranty.Warranty= '';
		testWarranty.QualityControl= '';
		testWarranty.Penality= '';
		testWarranty.Packing= '';
		testWarranty.SpecialWarranty= '';
    }
}

The error I'm getting is 

 classes/APTS_OrderRequestTest.cls: Variable does not exist: testWarranty (Line: 32, Column: 28)
classes/APTS_OrderRequestTest.cls: Illegal assignment from String to APTS_OrderRequest.PaymentNDeliveryTerms (Line: 34, Column: 9)
classes/APTS_OrderRequestTest.cls: Illegal assignment from String to List<APTS_OrderRequest.Links> (Line: 35, Column: 9)
classes/APTS_OrderRequestTest.cls: Illegal assignment from String to List<APTS_OrderRequest.OrderLineItems> (Line: 36, Column: 9)
classes/APTS_OrderRequestTest.cls: Illegal assignment from String to List<APTS_OrderRequest.Addresses> (Line: 37, Column: 9)
classes/APTS_OrderRequestTest.cls: Illegal assignment from String to List<APTS_OrderRequest.MileStoneBillingPlan> (Line: 38, Column: 9)
classes/APTS_OrderRequestTest.cls: Illegal assignment from String to List<APTS_OrderRequest.SalesPersonDetails> (Line: 39, Column: 9)
classes/APTS_OrderRequestTest.cls: Invalid type: APTS_OrderRequest.Order.Warranty (Line: 40, Column: 9)
classes/APTS_OrderRequestTest.cls: Variable does not exist: testWarranty (Line: 41, Column: 9)
classes/APTS_OrderRequestTest.cls: Variable does not exist: testWarranty (Line: 42, Column: 9)

Kiran PandiyanKiran Pandiyan

line number 34 was actually 

objOrder.Warranty= testwarranty;

Saravana Muthu 8Saravana Muthu 8
Hi,

I think need to change this line from public Warranty Warranty{get;set;} to public String Warranty{get;set;}.

Please try this ane let me know if it works.

Thanks,
Sarav
Sweet Potato Tec
Kiran PandiyanKiran Pandiyan

@Saravana Muthu8 

I don't think I could do it. I can only change the test class not the Request Class

Saravana Muthu 8Saravana Muthu 8
Ok try like this in test class objOrder.Warranty= 'testwarranty'; If it works let me please

Thanks,
Sarav
Sweet Potato tec
Kiran PandiyanKiran Pandiyan

@Saravana

No that also is not working.
I also tried leaving empty and just declaring the variable. Still the error is popping.

Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

sample Test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html


sample test class to start
Try below test class you will get 95% code coverage
@isTest
public class APTS_OrderRequestTest {

     public static testmethod void testMethod1() {
        APTS_OrderRequest objRequest = new APTS_OrderRequest();
        
        APTS_OrderRequest.Order objOrder = new APTS_OrderRequest.Order();
        objOrder.OrderSource= '';
        objOrder.PurchaseOrderNumber= '';
        objOrder.POOrderDate= '';
        objOrder.SalesResponsibleUnit= '';
        objOrder.QuotationReference= '';
        objOrder.ApttusQuotationID= '';
        objOrder.QuotationRevisionNo= '';
        objOrder.QuoteCreationDate= '';
        objOrder.QuoteReferenceDescription= '';
        objOrder.EndUserCustomerIdentifier= '';
        objOrder.CustomerGUID= '';
        objOrder.PlacedBy= '';
        objOrder.ProjectName= '';
        objOrder.CurrencyCode= '';
        objOrder.SourceSystemOrderNumber= '';
        objOrder.OrderDate= '';
        objOrder.OrderingSystem= '';
        objOrder.HandledBy= '';
        objOrder.AdditionalSalesPersonEmail= '';
        objOrder.ExpectedDeliverDate= '';
        objOrder.IndustryUsage= '';
        objOrder.ChannelClass= '';
        objOrder.TenderLocationLevel2ID= '';
        objOrder.CustomerReference= '';
        objOrder.Application= '';
        objOrder.IndustryUsageLevel1= '';
        objOrder.IndustryUsageLevel2= '';


        APTS_OrderRequest.Warranty testWarranty = new APTS_OrderRequest.Warranty();
        testWarranty.WarrantyMax= '';
        testWarranty.Warranty= '';
        testWarranty.QualityControl= '';
        testWarranty.Penalty= '';
        testWarranty.Packing= '';
        testWarranty.SpecialWarranty= '';
        objOrder.Warranty= testWarranty ;
        
        APTS_OrderRequest.ConfiguredItem ConfiguredItem = new APTS_OrderRequest.ConfiguredItem();
        ConfiguredItem.ConfigurationId='';
        ConfiguredItem.NomenclatureNumber='';
        ConfiguredItem.ConfiguratorId='';
        ConfiguredItem.ConfiguratorPlatform='';

        APTS_OrderRequest.OrderLineItems OrderLineItems = new APTS_OrderRequest.OrderLineItems();
        OrderLineItems.SourceSystemItemId=''; 
        OrderLineItems.ApttusQuotationLineID=''; 
        OrderLineItems.LineNumber=1; 
        OrderLineItems.ItemSequence=1; 
        OrderLineItems.ProductID=''; 
        OrderLineItems.ProductNumber=''; 
        OrderLineItems.ProductDescription=''; 
        OrderLineItems.PartID=''; 
        OrderLineItems.ProductType=''; 
        OrderLineItems.TechnicalID=''; 
        OrderLineItems.ClassificationID=''; 
        OrderLineItems.StandardCategoryType=''; 
        OrderLineItems.CategoryID=''; 
        OrderLineItems.Quantity=1; 
        OrderLineItems.CurrencyCode=''; 
        OrderLineItems.UnitOfMeasure=''; 
        OrderLineItems.UnitPrice=1; 
        OrderLineItems.DeliveryDate=''; 
        OrderLineItems.ContractDeliveryDate=''; 
        OrderLineItems.ManualLeadTime=1; 
        OrderLineItems.ProductionUnit=''; 
        OrderLineItems.ListPrice=1; 
        OrderLineItems.TotalNetSalesPrice=1; 
        OrderLineItems.NetSalesPrice=1; 
        OrderLineItems.TransferPrice=1; 
        OrderLineItems.TransferPriceCurrency=''; 
        OrderLineItems.StandardDiscount=1; 
        OrderLineItems.DiscountPercentage=1; 
        OrderLineItems.ParentLineNumber=''; 
        OrderLineItems.RequiredLeadTime=''; 
        OrderLineItems.BaseCost=1; 
        OrderLineItems.AdditionalDiscount=1; 
        OrderLineItems.Supplier=''; 
        OrderLineItems.Customer=''; 
        OrderLineItems.EndUserCountry=''; 
        OrderLineItems.PurchaseOrderNumber=''; 
        OrderLineItems.PurchaseOrderLineNumber=''; 
        OrderLineItems.TransportMode=''; 
        OrderLineItems.ConfiguredItem=ConfiguredItem; 
        
    }
}

Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .

Let us know if this will help you