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
Archana Lal 2Archana Lal 2 

Hi,Can anybody please help me to write a test class that gives a atleat 75% code Coverage

Hi All,
Can anybody help me to write a test class for the below apex class.
public class MobileShopeeClass{
public static void NewCustomerDiscount(list<Mobile__c>MobileList1){
for(Mobile__c m:MobileList1){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*10)/100;
m.Discount_status__c='You have got 10 % Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Price__c=m.Price__c-(m.Price__c*20)/100;
m.Discount_status__c='You have got 20 % Discount!!';
}
}
}
public static void OldCustomerDiscount(list<Mobile__c>MobileList2){
for(Mobile__c m:MobileList2){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*5)/100;
m.Discount_status__c='You have got 5% Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Discount_status__c='Sorry,no discount!!';
}
}
}
Best Answer chosen by Archana Lal 2
Abdul KhatriAbdul Khatri
After reveiwing youir trigger, the following code fit best from all perspective
 
@isTest
public class MobileShopeeClass_Test {

    private static testMethod void test_newcustomerdiscount() 
    {  
        
        List<Mobile__c> mobileToInsertList = createTestData();
        insert mobileToInsertList;

        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c='Suma' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('10 % Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('20 % Discount'));

    }
    
	private static testMethod void test_oldcustomerdiscount() 
	{
        List<Mobile__c> mobileToInsertList = createTestData();
        insert mobileToInsertList;
        
        update mobileToInsertList;
	
        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Suma' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('5% Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('no discount'));
 
    }

    
    public static List<Mobile__c> createTestData()
    {
        List<Mobile__c> mobileToInsertList = new List<Mobile__c> {
            new Mobile__c (Customer_Name__c = 'Suma',Brand__c = 'Samsung', Price__c = 100.00),
            new Mobile__c (Customer_Name__c = 'Gopan',Brand__c = 'Apple', Price__c = 200.00)
        };
        return mobileToInsertList;        
    }
}

 

All Answers

Abdul KhatriAbdul Khatri
One thing in your class, there is no DML Operation Like insert, update etc. I believe you may be calling out this in Before triggers. In that case it is fine not to have DML Operation but if you are running After Trigger make sure you have proper DML Operation applied otherwise Test will fail.

Here is the test class
 
@isTest
public class MobileShopeeClass_Test {

    @TestSetup
    static void setup(){
        
        List<Mobile__c> mobileToInsertList = new List<Mobile__c> {
        	new Mobile__c (Name = 'Samsung Mobile',Brand__c = 'Samsung', Price__c = 100.00),
            new Mobile__c (Name = 'Apple Mobile',Brand__c = 'Apple', Price__c = 200.00)
        };
        insert mobileToInsertList;
        
    }
    
    private static testMethod void test_newcustomerdiscount() {
        
        List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
        
        MobileShopeeClass.NewCustomerDiscount(mobileList);
        
        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Name = 'Samsung Mobile' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Name = 'Apple Mobile' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('10 % Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('20 % Discount'));
    }
    
    private static testMethod void test_oldcustomerdiscount() {
        
        List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
        
        MobileShopeeClass.OldCustomerDiscount(mobileList);
        
        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Name = 'Samsung Mobile' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Name = 'Apple Mobile' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('5% Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('no discount'));
    }    
}



 
Archana Lal 2Archana Lal 2
Hi Adbul,
Thanks a lot on the answer ,but i have one more to clarify,i just want to mention that i have written a trigger for this.I this case do i need to write the test class like this.
trigger MobileShopee on Mobile__c (Before Insert,Before Update) {
if(Trigger.isInsert==true){
MobileShopeeClass.NewCustomerDiscount(Trigger.New);
if(trigger.isUpdate==true){
MobileShopeeClass.OldCustomerDiscount(Trigger.New);

Thanks & Regards,
Archana Lal
Abdul KhatriAbdul Khatri
I am not sure if I understood you quertion.

My test class will cover both your class and you trigger.

You trigger looks good to me.

If my test class help you, please dont' forget to mark it best.
Archana Lal 2Archana Lal 2
I am getting this error message while doing run test.
Error MessageSystem.QueryException: List has no rows for assignment to SObject
Stack TraceClass.MobileShopeeClass_Test.test_oldcustomerdiscount: line 53, column 
 
Abdul KhatriAbdul Khatri
Have you made any changes or running the test as is?
Archana Lal 2Archana Lal 2
yes the field name ,name .i changed to customer_name_c
Abdul KhatriAbdul Khatri
Please share the changed code?
Archana Lal 2Archana Lal 2
@isTest
public class MobileShopeeClass_Test {
@TestSetup
static void setup(){
        List<Mobile__c> mobileToInsertList = new List<Mobile__c> {

            new Mobile__c (Customer_Name__c = 'Suma',Brand__c = 'Samsung', Price__c = 100.00),

            new Mobile__c (Customer_Name__c = 'Gopan',Brand__c = 'Apple', Price__c = 200.00)

        };
        insert mobileToInsertList;
      
 }

      private static testMethod void test_newcustomerdiscount() {

         
        List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
         

        MobileShopeeClass.NewCustomerDiscount(mobileList);
         

        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c='Suma ' LIMIT 1];

        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

 

        system.assert(mobileSamsung.Discount_status__c.contains ('10 % Discount'));

        system.assert(mobileApple.Discount_status__c.contains ('20 % Discount'));

    }
private static testMethod void test_oldcustomerdiscount() {
   

        List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];

         

        MobileShopeeClass.OldCustomerDiscount(mobileList);

         

        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Suma' LIMIT 1];

        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' Mobile' LIMIT 1];

 

        system.assert(mobileSamsung.Discount_status__c.contains ('5% Discount'));

        system.assert(mobileApple.Discount_status__c.contains ('no discount'));
 
    }

   
     

}
Abdul KhatriAbdul Khatri
Please use the following code. There were few following mistakes in your code which I fixed and it is working now
User-added image
User-added image
@isTest
public class MobileShopeeClass_Test {

	@TestSetup
	static void setup(){
        List<Mobile__c> mobileToInsertList = new List<Mobile__c> {
            new Mobile__c (Customer_Name__c = 'Suma',Brand__c = 'Samsung', Price__c = 100.00),
            new Mobile__c (Customer_Name__c = 'Gopan',Brand__c = 'Apple', Price__c = 200.00)
        };
        insert mobileToInsertList;     
    }

    private static testMethod void test_newcustomerdiscount() 
    {
		List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
        MobileShopeeClass.NewCustomerDiscount(mobileList);      

        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c='Suma' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('10 % Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('20 % Discount'));

    }
    
	private static testMethod void test_oldcustomerdiscount() 
	{
		List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
	
        MobileShopeeClass.OldCustomerDiscount(mobileList);

        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Suma' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('5% Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('no discount'));
 
    }

}

 
Archana Lal 2Archana Lal 2
still it is showing a error like this.
Error MessageSystem.AssertException: Assertion Failed
Stack TraceClass.MobileShopeeClass_Test.test_oldcustomerdiscount: line 35, column 1
Abdul KhatriAbdul Khatri
After reveiwing youir trigger, the following code fit best from all perspective
 
@isTest
public class MobileShopeeClass_Test {

    private static testMethod void test_newcustomerdiscount() 
    {  
        
        List<Mobile__c> mobileToInsertList = createTestData();
        insert mobileToInsertList;

        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c='Suma' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('10 % Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('20 % Discount'));

    }
    
	private static testMethod void test_oldcustomerdiscount() 
	{
        List<Mobile__c> mobileToInsertList = createTestData();
        insert mobileToInsertList;
        
        update mobileToInsertList;
	
        Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Suma' LIMIT 1];
        Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];

        system.assert(mobileSamsung.Discount_status__c.contains ('5% Discount'));
        system.assert(mobileApple.Discount_status__c.contains ('no discount'));
 
    }

    
    public static List<Mobile__c> createTestData()
    {
        List<Mobile__c> mobileToInsertList = new List<Mobile__c> {
            new Mobile__c (Customer_Name__c = 'Suma',Brand__c = 'Samsung', Price__c = 100.00),
            new Mobile__c (Customer_Name__c = 'Gopan',Brand__c = 'Apple', Price__c = 200.00)
        };
        return mobileToInsertList;        
    }
}

 
This was selected as the best answer
Archana Lal 2Archana Lal 2
Thanks a lot for you effort.i don't know it shows the same error for the olddiscountstatus function.
Abdul KhatriAbdul Khatri
Please make sure that you retreive the record with proper string in the where clause. Again, did you made any change?
Archana Lal 2Archana Lal 2
no change i made
Abdul KhatriAbdul Khatri
which line from my code asser failure
Archana Lal 2Archana Lal 2
Error MessageSystem.AssertException: Assertion Failed
Stack TraceClass.MobileShopeeClass_Test.test_oldcustomerdiscount: line 28, column 1
Archana Lal 2Archana Lal 2
Error Message System.AssertException: Assertion Failed Stack Trace Class.MobileShopeeClass_Test.test_oldcustomerdiscount: line 28, column 1
Abdul KhatriAbdul Khatri
Can you share your final version of the class MobileShopeeClass?
Archana Lal 2Archana Lal 2
public class MobileShopeeClass{
public static void NewCustomerDiscount(list<Mobile__c>MobileList){
for(Mobile__c m:MobileList){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*10)/100;
m.Discount_status__c='You have got 10 % Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Price__c=m.Price__c-(m.Price__c*20)/100;
m.Discount_status__c='You have got 20 % Discount!!';
}
}
}
public static void OldCustomerDiscount(list<Mobile__c>MobileList){
for(Mobile__c m:MobileList){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*5)/100;
m.Discount_status__c='You have got 5% Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Discount_status__c='Sorry,no discount!!';
}
}
}
}
Abdul KhatriAbdul Khatri
I do not see any issues. The test class must run successfully.
Archana Lal 2Archana Lal 2
Hi Abdul,
Thanks a lot for the help.