You need to sign in to do that
Don't have an account?
Anonymous Developer
Test Class Creation Need Help!
This is my code:
Thanks in Advance
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
// 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
// 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.