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
srikanth cheera 6srikanth cheera 6 

Any one can write test code for this

Any one can write test code for this 
=============================
public class Task04{
public static void beforeinsert(list<Opportunity> opo){
for(Opportunity op:opo){
if(op.name!=null){
op.stagename='prospecting';
op.closedate=system.today()+15;
}
}
}
}
====
Thanks
Suraj TripathiSuraj Tripathi
Hi Srikanth,

You can use this code for your test class 
 
@isTest private class Task04Test
{
    @isTest static void m1()
    {
        List<Opportunity> opplist = new List<Opportunity>();
        Opportunity opp =new Opportunity();
        opp.Name= 'Test Opp';
        opp.CloseDate = System.today().addDays(15);
        opp.StageName = 'Prospecting';
        opplist.add(opp);
        insert opplist;
    }
}


If it helps you, Kindly mark this answer as best, so that it will be removed from unsolved qestions
Regards
Suraj
srikanth cheera 6srikanth cheera 6
Hi suraj the code was not working showing 0% 
Suraj TripathiSuraj Tripathi

But it is working fine with 100% coverage in my org


Make Sure You are checking coverage for your trigger and its handler, not for test class itself.

You can run your test class from here, but for coverage, we always check coverage on Main Class and Trigger Code

User-added image




You can see here my Apex Class and Trigger both are covered with 100% code coverage

//Trigger_Handler is covering with 100% coverage, you can check it.

User-added image


//Trigger is also covered with 100% coverage

User-added image



Now, if you have any doubt, please feel free to ask me 


Regards
Suraj

srikanth cheera 6srikanth cheera 6
ok suraj bro it's working....my main mistake is apex class and trigger class names are name maybe dats pbm 
 
srikanth cheera 6srikanth cheera 6
And one more...test map 
====================
public class Task14 {
 
    public static void beforeupdate(Map<id,Account> oldmap,Map<id,Account> newmap){
        list<id> accid=new list<id>();
        for(id key:oldmap.keySet()){
            Account old=oldmap.get(key);
            Account newmp=newmap.get(key);
            if(old.Phone!=newmp.Phone){
              accid.add(key);
            }
        }
        list<Contact> con=[select lastname,firstname,phone,accountid from Contact where accountid in:accid];
        for(Contact c:con){
            Account a=newmap.get(c.accountid);
            Account b=oldmap.get(c.accountid);
            c.Phone=a.Phone;
            c.OtherPhone=b.phone;
            
        }
        update con;
    }
}
==========================================================

trigger Task14Trigger on Account (before update) {
    if(trigger.isbefore && trigger.isupdate){
        Task14.beforeupdate(Trigger.oldmap, Trigger.newmap);
    }
}
====================================================================
@istest
private class Task14Test {
    @istest static void test(){
        
        Account acc=new Account();
        acc.name='sree';
        acc.Phone='7569';
        test.startTest();
        Task14 ta=new Task14();
        insert acc;
        test.stopTest();
        
        Contact con=new Contact();
        con.lastname='eers';
        con.Phone='7569';
        con.OtherPhone='123';
        con.AccountId=acc.id;
        
        acc.phone='456';
        test.startTest();
        update acc;
        test.stoptest();
        
      
        system.assertEquals(acc.phone, con.phone);
        system.assertEquals(acc.phone, con.OtherPhone);
    }
}
==================================================
srikanth cheera 6srikanth cheera 6
solve the test class bro
Suraj TripathiSuraj Tripathi
Hi Srikanth,

This is test class for your second code
with 100% coverage
 
@istest
private class Task14Test {
    @istest static void testmethod1()
    {    
        Account acc=new Account();
        acc.name='sree';
        acc.Phone='7569';
        insert acc;
       
        Contact con=new Contact();
        con.firstname = 'testname';
        con.lastname='eers';
        con.Phone=acc.Phone;
        con.OtherPhone='123';
        con.AccountId=acc.id;
        insert con;
        
       	acc.phone = '667788';
        update acc;
   
    }
}

If this resolve your problem, then kindly please mark this answer as best answer,

Regards
Suraj

 
srikanth cheera 6srikanth cheera 6
Write a batch apex to fetch all the tasks assigned to the user and set the status as closed with test class.