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
PappuPappu 

Test class for STOWD API

Hi All,

Any help in writting test class for the below trigger is appreciated.

trigger test on Mail__c (after update) {
            list<Duration__c> durations = new list<Duration__c>();
            set<string> trackedFields = new set<string> {'OwnerId', 'Status__c', 'Sub_Status__c'}; 
            STOWD.API.CalculateDurations(durations, trackedFields, 'Request__History', 'ParentId'); 
            database.insert(durations, false);
    }
Best Answer chosen by Pappu
Niraj Kr SinghNiraj Kr Singh
Try this it will work:
 
@istest
private class TestClass {
   @istest static void  testClassMethod() {
        List<Mail__c> lstMails = new List<Mail__c>();
		for(Integer index=1; index <= 5; index++) {
			Mail__c objMail = new Mail__c();
			objMail.Name = 'TestMail-' + index;
			//Add all required fields here...
        }
		
		Insert lstMails;
		
		List<Mail__c> lstData = [SELECT Id, Name FROM Mail__c];
		system.assertEquals(lstData.size(), 5);
		
		for(Mail__c objMall : lstData) {
			objMall.Name = objMall.Name + ' -Update';
		}
		
		Test.startTest();
			update lstData;
		Test.stopTest();
    }
}
My mistake, I forget to add static keyword in method.
 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Pappu,

Plz try this and let me know if it works
Might be some update needed according to your requirements
@istest
private class TestClass {
   @istest private void testMethod() {
        List<Mail__c> lstMails = new List<Mail__c>();
		for(Integer index=1; index <= 5; index++) {
			Mail__c objMail = new Mail__c();
			objMail.Name = 'TestMail-' + index;
			//Add all required fields here...
        }
		
		Insert lstMails;
		
		List<Mail__c> lstData = [SELECT Id, Name FROM Mail__c];
		system.assertEquals(lstData.size(), 5);
		
		for(Mail__c objMall : lstData) {
			objMall.Name = objMall.Name + ' -Update';
		}
		
		Test.startTest();
			update lstData;
		Test.stopTest();
    }
}

Thanks
Niraj

 
PappuPappu
Thanks for the reply Niraj.

I am getting "Unexpected token 'testMethod'." error while saving the  test class. I just copied your code and pasted to my org.
Niraj Kr SinghNiraj Kr Singh
Sorry Pappu.
testMethod is a keyword in Salesforce.

Replace this line :
@istest private void testMethod() {

By:
@istest private void testClassMethod1() {



 
PappuPappu
Thanks again Niraj!! I've changed as u said.
 
Now saving test class is fine. When executing "Run test" I m getting the below meeage. I already added test methods to my class.
No idea whats the issue here !

User-added image
Niraj Kr SinghNiraj Kr Singh
Try this it will work:
 
@istest
private class TestClass {
   @istest static void  testClassMethod() {
        List<Mail__c> lstMails = new List<Mail__c>();
		for(Integer index=1; index <= 5; index++) {
			Mail__c objMail = new Mail__c();
			objMail.Name = 'TestMail-' + index;
			//Add all required fields here...
        }
		
		Insert lstMails;
		
		List<Mail__c> lstData = [SELECT Id, Name FROM Mail__c];
		system.assertEquals(lstData.size(), 5);
		
		for(Mail__c objMall : lstData) {
			objMall.Name = objMall.Name + ' -Update';
		}
		
		Test.startTest();
			update lstData;
		Test.stopTest();
    }
}
My mistake, I forget to add static keyword in method.
 
This was selected as the best answer
PappuPappu
It worked as expected when I add static testmethod keyword. Thanks Niraj :) !!
PappuPappu
Can you help me to undersand more about the method - "  STOWD.API.CalculateDurations". 
Code I shared earlier was existing and I dont have an idea about flow of the code.
So little curious to know the functions and methods used in it.