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
smita bhargavasmita bhargava 

test class for creating new opportunity

Hello
I am new to writing test class. I need help in writing test class for the following code.
scenairo: when I create a new account from UI, a nee opportunity record gets created under the account.
public class CreateNewOpportunity
{
   public List<Opportunity> oppList=new List<Opportunity>();
   
   public void CreateOpportunity(List<Account> accList)
   {
     for(Account a:accList)
     {
       Opportunity o = new Opportunity();
       o.AccountID=a.ID;
       
       o.Name=a.Name;
       o.CloseDate=System.Today().addDays(3);
       
       o.StageName='Prospecting';
       
       oppList.add(o);
     }
     
     if (oppList.size() > 0)
         Database.Insert(oppList);
   }
}

trigger trg_createnewoppor on Account (after Insert,after update)
{
  if ((Trigger.isInsert) || (Trigger.IsBefore))
  {
    CreateNewOpportunity o = new CreateNewOpportunity();
    o.CreateOpportunity(Trigger.New);
  }
}


Thanks
smita
Best Answer chosen by smita bhargava
Sitanshu TripathiSitanshu Tripathi
@isTest
public class CreateNewOpportunityTest
{
    @isTest
    public static void CreateOpportunityTestMethod()
    {
        Account objAccount = new Account();
        objAccount.Name = 'Test Acc';
        insert objAccount;
        System.AssertEquals(objAccount.Name, 'Test Acc');
        
        Opportunity objOpportunity = new Opportunity();
        objOpportunity.Name = 'Test Opp';
        objOpportunity.Accountid = objAccount.id;
        objOpportunity.StageName = 'Prospecting';
        objOpportunity.CloseDate = system.Today()+3;
        insert objOpportunity;
        System.AssertEquals(objOpportunity.Name, 'Test Opp');
    }
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

 

All Answers

YogeshMoreYogeshMore
Hi Smita,
 
You can use following test class for you trigger and handler.
Copy this code and create new class. Then run this test class and check the code coverage.
 
@IsTest
private class CreateNewOpportunity_Test{
	
	static testMethod void testMethod_1(){
		Account acc = new Account();
		acc.Name = 'TestAccount';
		insert acc;
		System.assert('TestAccount', acc.Name);
	}
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More

Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || Skype:-yogesh.more44​
 
Sitanshu TripathiSitanshu Tripathi
@isTest
public class CreateNewOpportunityTest
{
    @isTest
    public static void CreateOpportunityTestMethod()
    {
        Account objAccount = new Account();
        objAccount.Name = 'Test Acc';
        insert objAccount;
        System.AssertEquals(objAccount.Name, 'Test Acc');
        
        Opportunity objOpportunity = new Opportunity();
        objOpportunity.Name = 'Test Opp';
        objOpportunity.Accountid = objAccount.id;
        objOpportunity.StageName = 'Prospecting';
        objOpportunity.CloseDate = system.Today()+3;
        insert objOpportunity;
        System.AssertEquals(objOpportunity.Name, 'Test Opp');
    }
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

 
This was selected as the best answer
Soene IbaniboSoene Ibanibo
Please i'm new to visualforce and  Apex classes. I need help displaying a specific record from a custom object on pdf format using the record id or a unique identifier as parameters. righ now i can only display one record and its awrong record not related to the current page for purchase order See my code below and help me make corrections thank:
public class contractorController {
 public Contractor_LPO__c purchaseList {get;set;} 
public string recordId {get;set;}

public contractorController () { recordId = Apexpages.currentPage().getParameters().get('Id'); purchaseList =new Contractor_LPO__c(); purchaseList = [select Id, Contractor_Quote__r.Contractor_Account__c, Contractor_Quote__r.id , Contractor_Quote__r.Amount_in_Words__c, Contractor_LPO__c.Name, Amount_in_Words__c, Contractor_Quote_Approved_Amount__c, Contractor_Quote_Status__c, Contractor_RFQ_Number__c, LPO_Date__c, Payment_Terms_and_Condition__c, Quantity__c, Quote_Description__c , Contractor_Quote__c,Status__c, Tax_Inclusive_Amount__c,Value_Added_Tax__c , VAT_Inclusive__c, Contractor_Account__c from Contractor_LPO__c limit 1]; } }
Yuvaraj mayilsamy 9Yuvaraj mayilsamy 9
Hi Smitha,

 The reason for writing test classes is not only to get code coverage but also to check whether we are getting the expected outcome. The previous answers may provide 100 percent code coverage but no one tested the outcome. Below is the code. Use this you can see 100percent code coverage in you class and in trigger.
 
@istest
public class CreateNewOpportunitytest {
    @istest static void newaccinsertnewopportunity(){
        //creating new account
          
        account newacc = new account(name = 'Sample Account');
        insert newacc;
        
        // we are checking whether the inserted account created the opportunity
        
        list<opportunity> queriedopp = [select id from opportunity where accountid =:newacc.id];
        
        // checking whether the query returned one result which confirms that inserting account inserts opportunity.
        // 
        system.assertEquals(1, queriedopp.size());
    }
}

Please mark it as the best answer if it helps.

Yuvaraj
smita bhargavasmita bhargava
Hi yuaraj
I tested your code, working fine and getting 100% code coverage. I have a doubt.

From developer point of view how do we know that this test class(CreateNewOpportunitytest) is asscoiated with the apex  class (CreateNewOpportunity)?

Because in test class we are not calling any method of apex class (CreateNewOpportunity).

I hope I am clear.

Thanks
smita
 
Sitanshu TripathiSitanshu Tripathi
Dear Smita,
This question is very confused for new developer, but I'll tell you how it's work for class step by step.
1. When you create a Test class, it go and check the trigger.
2. When trigger is fire then trigger checked that how many classes called in that trigger dml.
3. After that all of the classes run for get coverage.

I think this thing never tell you anyone, if you don't ask this question.
I'm also a fresher in this technology.

Please mark this answer as SOLVED and BEST ANSWER if it helps you, I was also give you a simple test class above.

Regards,
Sitanshu Tripathi

https://sitanshusfdc.blogspot.in/
smita bhargavasmita bhargava
Hi yuaraj & sitanshu
I checked both ur codes but right when I run the test class now I am getting error as "Method defined as testMethod do not support webservice
callouts'".

can u pls le tme know how to take care of this?

thanks
smita
smita bhargavasmita bhargava
Hi 
I am getting the code coverage as 100% for apex class and trigger, but I fail to understand why the test class is failing and it shows the error as mentioned previously..

 
Sitanshu TripathiSitanshu Tripathi
Hi Smita,
But I tried just now in my dev org and I'm not facing any error. It maybe some your org issue.
Will you please try in fresh org ?