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
Anonymous DeveloperAnonymous Developer 

Test Class Creation Need Help!

This is my code:
 
public without sharing class AS_TriggerQuoteEvent {

    @InvocableMethod( label = 'Trigger Quote')

    public static void triggerQuoteEvent(List<Request> requestList) {
        system.debug('requestList >>' +requestList);

        for(Request req : requestList){
            if ( req.event != null ){
                Database.update ( req.event );
            } else {
                Database.update ( req.events );
            }
        }
    }

    public class Request {

        @InvocableVariable( label = 'Events')
        public List<sObject> events;
        
        @InvocableVariable( label = 'Event')
        public sObject event;
    }
}

Thanks in Advance
Best Answer chosen by Anonymous Developer
Arun Kumar 1141Arun Kumar 1141

// test class

@isTest
private class AS_TriggerQuoteEventTest {

    @isTest
    static void testTriggerQuoteEvent() {

        Opportunity opp = new Opportunity(Name = 'Test', StageName = 'Prospecting', CloseDate = Date.today());
        insert opp;

        AS_TriggerQuoteEvent.Request req1 = new AS_TriggerQuoteEvent.Request();
        req1.event = opp;

        List<sObject> eventList = new List<sObject>{ opp };
        AS_TriggerQuoteEvent.Request req2 = new AS_TriggerQuoteEvent.Request();
        req2.events = eventList;

        List<AS_TriggerQuoteEvent.Request> requestList = new List<AS_TriggerQuoteEvent.Request>{ req1, req2 };

        System.test.startTest();
        System.test.setMock(HttpCalloutMock.class, new AS_TriggerQuoteEventTestMock());
        AS_TriggerQuoteEvent.triggerQuoteEvent(requestList);
        System.test.stopTest();

        opp = [SELECT Id, Name, StageName FROM Opportunity WHERE Id = :opp.Id];
        System.assertEquals('Test', opp.Name);

    }
}

//mock class 

@isTest
global class AS_TriggerQuoteEventTestMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

 

Please mark this as best answer, if this helps.
 

All Answers

Nithyanandha NimmalaNithyanandha Nimmala
Is "Request" an sObject in this case
Arun Kumar 1141Arun Kumar 1141

// test class

@isTest
private class AS_TriggerQuoteEventTest {

    @isTest
    static void testTriggerQuoteEvent() {

        Opportunity opp = new Opportunity(Name = 'Test', StageName = 'Prospecting', CloseDate = Date.today());
        insert opp;

        AS_TriggerQuoteEvent.Request req1 = new AS_TriggerQuoteEvent.Request();
        req1.event = opp;

        List<sObject> eventList = new List<sObject>{ opp };
        AS_TriggerQuoteEvent.Request req2 = new AS_TriggerQuoteEvent.Request();
        req2.events = eventList;

        List<AS_TriggerQuoteEvent.Request> requestList = new List<AS_TriggerQuoteEvent.Request>{ req1, req2 };

        System.test.startTest();
        System.test.setMock(HttpCalloutMock.class, new AS_TriggerQuoteEventTestMock());
        AS_TriggerQuoteEvent.triggerQuoteEvent(requestList);
        System.test.stopTest();

        opp = [SELECT Id, Name, StageName FROM Opportunity WHERE Id = :opp.Id];
        System.assertEquals('Test', opp.Name);

    }
}

//mock class 

@isTest
global class AS_TriggerQuoteEventTestMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

 

Please mark this as best answer, if this helps.
 

This was selected as the best answer
Anonymous DeveloperAnonymous Developer
Thanks Arun