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
Compass 1Compass 1 

Apex Test Class

I am new in Apex Development. I have written one Apex Class but need help to create the test unit for this class so I can deploy in production.
For reference find below the code of my Apex Class.


public class topprospectsqry{

    public Opportunity[] getOpportunityList() {
    Opportunity[] opps = [SELECT
    name,
    account.name,
    Facility__c,
    Scope_Of_Work__c,
    Pricing_Type__c,
    Revenue__c,
    Contract_Award_Date__c,
    FROM
    Opportunity
    where
    Top_Prospect__c='Yes'
    order by Revenue__c desc
    LIMIT 20
    ];
    return opps;
    }
     
    public pageReference generateReport() {
    return Page.Thirdpage;
    }
    
}


 
logontokartiklogontokartik
Pleae take a look at the Salesforce documentation on how to write test classes,

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/

For your code above 
 
// Load the test data
Account acc = new Account(Name='Test Acc');
insert acc;

// Load the test opportunities
List<Opportunity> opps = new List<Opportunity>();
for(Integer i=0;i<200;i++){
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.StageName = 'Prospecting';
opp.CloseDate = Date.today();
opp.Name = 'Test opp ' + i;
opps.add(opp);
}
insert opps;

// Test start here

Test.startTest();

topprospectsqry testcls = new topprospectsqry();
List<Opportunity> returned = testcls.getOpportunityList();
System.assertEquals(200,returned.size());
Test.stopTest();

 
Compass 1Compass 1
Thanks for the response, with the test script you given I am getting the following error,

Error: Compile Error: expecting right curly bracket, found 'insert' at line 9 column 0

My test script,
-----------------

@isTest

private class topprospecttest{

 // Load the test data

Account acc = new Account(Name='Test Acc');

insert acc;

// Load the test opportunities

List<Opportunity> opps = new List<Opportunity>();

for(Integer i=0;i<200;i++){

Opportunity opp = new Opportunity();

opp.AccountId = acc.Id;

opp.StageName = 'Prospecting';

opp.CloseDate = Date.today();

opp.Name = 'Test opp ' + i;

opps.add(opp);

}

insert opps;

// Test start here
Test.startTest();

topprospectsqry testcls = new topprospectsqry();

List<Opportunity> returned = testcls.getOpportunityList();

System.assertEquals(200,returned.size());

Test.stopTest();

}


 
Compass 1Compass 1
I am able to manage execute after inserting the static methode. But now after testing its shows me 60%. what more change should i need to put to make it more than 75%?


----------------------------------------------------------------------------------------------------------
@isTest

private class topprospecttest{
static testMethod void verifyAccountDescriptionsWhereOverwritten(){

 // Load the test data
        Account acc = new Account();
        acc.Name='Test of the Account1';
        acc.type='Home';
        insert acc;

// Load the test opportunities

List<Opportunity> opps = new List<Opportunity>();
for(Integer i=0;i<200;i++)
{
    Opportunity opp = new Opportunity();
    opp.AccountId = acc.Id;
    opp.StageName = 'Prospecting';
    opp.CloseDate = Date.today();
    opp.Name = 'Test opp ' + i;
    opps.add(opp);
}

insert opps;

// Test start here
Test.startTest();
topprospectsqry testcls = new topprospectsqry();
List<Opportunity> returned = testcls.getOpportunityList();
System.assertEquals(200,returned.size());
Test.stopTest();

}
}

------------------------------------------------------------------------------------------------------