+ Start a Discussion
Russell Farmer 9Russell Farmer 9 

Apex Test Class - pass test data ID as string to method

I have an Apex Class that basically queries data and it takes opportunity ID as an input. I am trying to write test class that creates the opportunity in test data and then I need to get the ID and pass it to the method. 

Here's my code:

Main Class:
public class RFPDateController{

    @AuraEnabled
    public static List <Milestone__c> fetchDates(String oppId) {
        //Qyery 10 
        List<Milestone__c> dateList = [SELECT ID, Name, Milestone_Type__c, Milestone_Date__c, Opportunity_Related__c from Milestone__c where Opportunity_Related__c != null and Opportunity_Related__c =: oppId];
        //return lis
        return dateList;
    }
    
}

Test Class
 
@isTest
private class RFPDateControllerTEST {
    
    static private map<String, Schema.RecordTypeInfo> accRTmap = Account.SObjectType.getDescribe().getRecordTypeInfosByName();
    static private map<String, Schema.RecordTypeInfo> conRTmap = Contact.SObjectType.getDescribe().getRecordTypeInfosByName();
    //static private Opportunity opp = new Opportunity();
    
    
        @isTest
    	static void validateGetMst(){
        CreateTestData();
        Test.startTest();
        List<Opportunity> opId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1];
        List<Milestone__c> mstResult = new List<Milestone__c>();
        mstResult = RFPDateController.fetchDates(opId);
        Test.stopTest();
        System.assertEquals(1,mstResult.size());
    	}
    
    static private void CreateTestData(){
        
        //Customer ACCOUNT
        Account acc = new Account();
        acc.Name = 'Test Customer Account';
        acc.RecordTypeId = accRTmap.get('Customer').getRecordTypeId();
        acc.Physical_Location__c='True';
        acc.Distributor_Types__c = 'Broker';
        acc.ShippingStreet='100 Market Street';
        acc.ShippingState='Texas';
        acc.ShippingPostalCode='75428';
        acc.ShippingCity='Houston';
        insert acc;
        
        //Distributor ACCOUNT (3)
        Account acc1 = new Account();
        acc1.Name = 'Test Distributor Account 1';
        acc1.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc1.Physical_Location__c='True';
        acc1.Distributor_Types__c = 'Broker';
        acc1.ShippingStreet='10 Chester Rd';
        acc1.ShippingState='Pennsylvania';
        acc1.ShippingPostalCode='19103';
        acc1.ShippingCity='Philadelphia';
        insert acc1;
        
        Account acc2 = new Account();
        acc2.Name = 'Test Distributor Account 2';
        acc2.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc2.Physical_Location__c='True';
        acc2.Distributor_Types__c = 'Broker';
        acc2.ShippingStreet='12 Jump Street';
        acc2.ShippingState='New Jersey';
        acc2.ShippingPostalCode='07041';
        acc2.ShippingCity='Parsippany';
        insert acc2;
        
        Account acc3 = new Account();
        acc3.Name = 'Test Distributor Account 3';
        acc3.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc3.Physical_Location__c='True';
        acc3.Distributor_Types__c = 'Broker';
        acc3.ShippingStreet='Wayne';
        acc3.ShippingState='Texas';
        acc3.ShippingPostalCode='75428';
        acc3.ShippingCity='commerce';
        insert acc3;
        
        //Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Pending';
        opp.CloseDate = System.today() + 30;
        opp.AccountId = acc.id;
        opp.Distributor__c=acc3.id;
        opp.Effective_Date__c=system.today();
        opp.Date_Due_To_Sales_Rep__c=system.today();
        opp.Questionnaire__c =true;
        insert opp;
        
        //Milestone
        Milestone__c mstn = new Milestone__c();
        mstn.Opportunity_Related__c = opp.Id;
        mstn.Name = opp.Name;
        mstn.Milestone_Date__c = System.today() + 30;
        mstn.Milestone_Type__c = 'Due Date';
        insert mstn;
        
    }

}

​​​​​​​
Best Answer chosen by Russell Farmer 9
Russell Farmer 9Russell Farmer 9
I figured it out. I put this

        Opportunity opId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1];
        List<Milestone__c> mstResult = new List<Milestone__c>();
        mstResult = RFPDateController.fetchDates(opId.Id);