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
Alice JuAlice Ju 

Test.startTest() Variable test is used before it is declared

Hi there,
I'm trying to add Test.StartTest() in my test class, but I kept getting this error message.

"Error: Compile Error: Variable test is used before it is declared. at line 4 column 46"

I then go back and check if I have any class called "test" that override the test method, but I can't find any. Do you guys happen to know where the error comes from? If you can kindly give me some advice I'll very much apprecaite. Thank you!

User-added image
Best Answer chosen by Alice Ju
Pat PattersonPat Patterson
You use 'test' as a variable name towards the bottom of your method (Account test = new Account...). Change this to testAccount and it will probably work.

All Answers

Alice JuAlice Ju
///And this is my test class...


@isTest (seeAllData=true)
public class TestOpportunity{

static testMethod void validateOpportunity() {
// Set up some local variables
String opportunityName = 'My Opportunity';
String standardPriceBookId = '';
Pricebook2 StandardPriceBook= new Pricebook2( Name='test');
insert StandardPriceBook;       
LIST<PriceBook2> pb2Standard = [select Id from Pricebook2 where IsStandard=true];
standardPriceBookId = pb2Standard[0].Id;
system.debug('PriceBOokId '+standardPriceBookId);
       
// set up opp and Verify that the results are as expected.
Account a = new Account(Name = 'Germany test account', Region__c='Germany', Type = 'Customer');
insert a;
Test.Starttest();
Opportunity o = new Opportunity(AccountId=a.Id, Name=opportunityName,
                                        StageName='Prospecting', CloseDate=Date.today());
insert o;
//test.starttest();
Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id];
System.assertEquals(opportunityName, opp.Name);
       
// set up product2 and Verify that the results are as expected.
Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
//Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
//System.assertEquals('Test Product', p2ex.Name);
       
// set up PricebookEntry and Verify that the results are as expected.
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true, UseStandardPrice=FALSE);
insert pbe;
//PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
//System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);
       
// set up OpportunityLineItem and Verify that the results are as expected.
OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, TotalPrice=99);
insert oli;
//OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
//System.assertEquals(pbe.Id, oliex.PriceBookEntryId);           
       
// ---------- END SET UP (phew!) ---------- //     
        Date DateOfToday=system.today();
       Account act = new account(Name='Test', Region__c='Unassigned',
       Type='Other',
       OwnerId='005U0000001Bb2uIAC'
       );
        insert act;
       Opportunity opty = new Opportunity(Name='Test', AccountId=act.Id, Type='New Revenue',
                                         Focus__c='Functional', Forecast_Product__c='Parasoft Concerto',
                                         Local_Currency__c='GBP (British Pound)',
                                         Exchange_rate_USD_Currency__c = 0.6,
                                         Exchange_Rate__c = -1.0,
                                         Use_system_exchange_rate__c=FALSE,
                                         CloseDate=DateOfToday, StageName='P - Initial Call',
                                         Next_Steps__c='Test Next Step', Maintenance_Notes__c='Test Maint Note');
                                        
        insert opty;

     
        OpportunityLineItem Product1 = new OpportunityLineItem (OpportunityId = opty.id, PricebookEntryId= pbe.id,  UnitPrice=1.00,     Quantity=1, Local_Price__c=100);
        insert Product1;
        OpportunityLineItem Product2 = new OpportunityLineItem (OpportunityId = opty.id, PricebookEntryId= pbe.id,  UnitPrice=1.00,     Quantity=1, Local_Price__c=100);
        insert Product2;
        opty.Use_system_exchange_rate__c=true;
        //insert opty;
        update opty;                                       
       

       System.debug('Inserting Opportunity');

       // Insert book
      
       
       // Retrieve the new book
       Opportunity opty2 = [SELECT OwnerId, AccountId, Exchange_Rate__c, Exchange_rate_USD_Currency__c ,
                            Local_Currency_Forecast_Amount__c,Local_Currency_Invoiced_Amount__c
                            FROM Opportunity WHERE Id =:opty.Id];
       System.debug('After opportunity is inserted.');      
        system.assert(opty2.Exchange_Rate__c!= -1.0);
        system.debug('opty2.Exchange_Rate__c'+opty2.Exchange_Rate__c);
        system.assert(opty2.Exchange_rate_USD_Currency__c==Null);
        system.debug('opty2.Exchange_rate_USD_Currency__c'+opty2.Exchange_rate_USD_Currency__c);
        List<OpportunityLineItem> OpProduct= [SELECT UnitPrice from OpportunityLineItem where OpportunityId=:opty2.Id];
        for(integer i=0; i < OpProduct.size(); i++)
        {
            system.debug('Unit Price: '+OpProduct[i].UnitPrice);
            system.assert(OpProduct[i].UnitPrice!=Null);
        }       
        System.assert(opty2.Local_Currency_Forecast_Amount__c!=Null);
       // Test that the trigger correctly updated the price
       //Test that Exchang_Rate remain unchanged when use system exchange rate is turned on.       
        double temp = opty2.Exchange_Rate__c;
        opty2.Exchange_Rate__c = opty2.Exchange_Rate__c*10;
        update opty2;
        opty2 = [SELECT OwnerId, AccountId, Exchange_Rate__c, Exchange_rate_USD_Currency__c ,
                            Local_Currency_Forecast_Amount__c,Local_Currency_Invoiced_Amount__c,
                            Use_system_exchange_rate__c
                            FROM Opportunity WHERE Id =:opty.Id];
        system.assert(opty2.Exchange_Rate__c== temp);
        system.debug('opty2.Exchange_Rate__c'+opty2.Exchange_Rate__c);      
       //Test that Exchang_Rate overwriteable when use system exchange rate is turned off.               
        temp = opty2.Exchange_Rate__c;
        opty2.Exchange_Rate__c = opty2.Exchange_Rate__c*1.5;
        opty2.Use_system_exchange_rate__c=FALSE;
        update opty2;
        opty2 = [SELECT OwnerId, AccountId, Exchange_Rate__c, Exchange_rate_USD_Currency__c ,
                            Local_Currency_Forecast_Amount__c,Local_Currency_Invoiced_Amount__c,
                            Region_op__c
                            FROM Opportunity WHERE Id =:opty.Id];
        system.assert(opty2.Exchange_Rate__c!= temp);
        system.debug('opty2.Exchange_Rate__c'+opty2.Exchange_Rate__c);      

      //Test that Exchange_Rate == Null when region equals UK;
        Account test = new Account(Name = 'Singapore test account', Region__c='Singapore', Type = 'Customer');
        insert test;
        opty2.AccountId = test.Id;
        update opty2;
        system.assert(opty2.Exchange_Rate__c!= NULL);
        opty2.Use_system_exchange_rate__c=true; 
        update opty2;
        system.debug('opty2.Exchange_Rate__c = '+opty2.Exchange_Rate__c);
   
    }

}
Pat PattersonPat Patterson
You use 'test' as a variable name towards the bottom of your method (Account test = new Account...). Change this to testAccount and it will probably work.
This was selected as the best answer
Alice JuAlice Ju
Oh... that's embarrassing. But wow thank you so much for going through my code! Yes it works!